Return value from callback function in javascript?

I am using node.js and the Translate library . Can I do something like this?


function traduce(text){
    translate.text(text,function(err,result){
        return result;
    });
}

And then use the result? It always returns me "undefined". is there any way to use the result without this?:.


translate.text(text,function(err,result){
     // use result
     // some logic
});

+5
source share
3 answers

You are not executing a function, you are passing a link to an anonymous function. If you want to get the return value, execute it:

function traduce(text){
    translate.text(text, (function(err,result){
        return result;
    })());
}
+3
source

, , . , , node.js .

api google, . , , .

+3

30 . , , -, ? , node.js - "waitFor", . , ,

-1

All Articles