Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to wait for function to finish in JS

function firstFunction(_callback){
    // do some asynchronous work
    
   let x= _callback(20);                                                      
       console.log("x=>",x)                                                 
     console.log("fahm")   
}

function secondFunction(){
    // call first function and pass in a callback function which
    // first function runs when it has completed
    firstFunction(function(a) {
        let x=0;                                                                   
            for (i=0; i<a; i++) {
               x=x+1
            }
             return x;
    })
}
 
PREVIOUS NEXT
Tagged: #wait #function #finish #JS
ADD COMMENT
Topic
Name
4+2 =