javascript - return value after a promise -


i have javascript function want return value after return method. easier see explain

function getvalue(file){     var val;     lookupvalue(file).then(function(res){        val = res.val;     }     return val; } 

what best way promise. understand it, return val return before lookupvalue has done it's then, can't return res.val returning inner function.

the best way use promise returning function is, this

lookupvalue(file).then(function(res) {     // write code depends on `res.val`, here }); 

the function invokes asynchronous function cannot wait till async function returns value. because, invokes async function , executes rest of code in it. so, when async function returns value, not received same function invoked it.

so, general idea write code depends on return value of async function, in async function itself.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -