asynchronous - Javascript: return fires before promise completes -
function process(hugedirectory) { var title = hugedirectory.gettitleeachfile().then(function(caption){ console.log(caption); return caption; }); return title; }
i have ajax call calls method right now, return nothing.
console.log(caption)
displays correct , expected value. however, value not returned @ end of method.
since process
make use of asynchronous function calls, cannot return value method.
the solution kind of problem make use of callback functions given below
function process(hugedirectory, callback) { hugedirectory.gettitleeachfile().then(function(caption){ console.log(caption); callback(caption) }); } process(hugedirectory, function(title){ //do title })
in instead of returning title process
pass callback function process
getting called when async call completed , resulted title
value passed callback function.
Comments
Post a Comment