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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -