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 -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -