node.js - To execute the functions after the completion of previous functions in javascript -


please me out remove settimeout in following code

var filearray=readdir.readfilesfromdirectory(full_path);   settimeout(function(){ settimeout(function(){filearray.foreach(function (val,index,array) {       /* time taken statements          define arg1*/       ` settimeout(function () {         /* time taken statements          define arg2,arg3*/          connecttofirebase.linktofirebase(arg1,arg2,arg3);          settimeout(function() {             process.exit(0);},10000) ;         },500 );//function 4     });//function 3   },500);//function 2 },500);//function 1 

i want execution of following function after completion of previous functions () i.e., 1 one creates problem when previous function need more time execution

when sth this:

asynccall(); console.log("t"); 

the browser not wait until asyncevent completed. keeps going , deals function in routine.

 |  | asynccall(); ---------->finished  |                                (  |  ) (what want) \/                               (  |  ) console.log("test"); <-----| 

as cannot wait until event finished callbacks , event handlers invented. thats how callback looks like:

function async(callback){ //function executes callback(); //the passed code executed } 

to run function callback, do:

 async(function(){//code ran later}); 

event hanlers same:

 window.onload=function(){} 

because im not using node.js cannot tell how youre async events working. dive docs , find out :)


Comments