Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript wait for function to finish

/*
 * JavaScript is executed synchronously, meaning each
 * line of code is executed in the order it appears in.
 * To wait for a function to finish, simply call it...
 * Any code after it will be called after it has
 * finished.
 */
function fizzbuzz(n) {
	// (this is just the most common and easy to code version)
	if (n % 3 === 0 && n % 5 === 0) {
		return "fizzbuzz";
	} else if (n % 3 === 0) {
		return "fizz";
	} else if (n % 5 === 0) {
		return "buzz";
	}
	return n;
}

console.log(fizzbuzz(15));
console.log("This code is run after `fizzbuzz` finishes");

/*
 * The only exception to the above, is asynchronous
 * functions. An asynchronous function is not run in sync
 * with the rest of the code which is often useful. To
 * run an asynchronous function synchronously, you can
 * use the await keyword. Alternatively (and probably
 * better in most contexts) you can use the `then` and
 * `catch` methods of the promise returned by an
 * asynchronous function (or just use a promise instead
 * of an asynchronous function) to get the value returned
 * by an asynchronous function and exectute code after it
 * has run.
 */
async function fizzbuzz(n) {
	if (n % 3 === 0 && n % 5 === 0) {
		return "fizzbuzz";
	} else if (n % 3 === 0) {
		return "fizz";
	} else if (n % 5 === 0) {
		return "buzz";
	}
	throw new Error("Nice catch!");
}

fizzbuzz(15).then((res) => {
	console.log(res);
	console.log("This is run after `fizzbuzz` finishes (and so is the above `console.log`...)");
}).catch((err) => {
	console.error(err);
	console.log("This is run after `fizzbuzz` finishes (and so is the above `console.error`...)");
});






// Bonus, a more concise fizzbuzz:
const fizzbuzz = ( n ) => n % 15 ? n % 3 ? n % 5 ? n : "buzz" : "fizz" : "fizzbuzz";
Comment

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;
    })
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: react video 
Javascript :: what is $ in jquery 
Javascript :: javascript fetch APIjson 
Javascript :: convert celsius to fahrenheit javascript 
Javascript :: inline javascript modules 
Javascript :: Initialize Axios React Redux CRUD API calls 
Javascript :: jquery ajax methods 
Javascript :: how to create a slice of the array with n elements taken from the beginning in javascript 
Javascript :: usehistory() hook 
Javascript :: xpath in javascript 
Javascript :: jalali moment get milisocnds 
Javascript :: vue 3 custom input component 
Javascript :: how to check if a key is present in a dictionary in js 
Javascript :: node js clear cache 
Javascript :: axios error handling 
Javascript :: javascript best way to loop through array 
Javascript :: node js require file in parent directory 
Javascript :: how to run cypress test 
Javascript :: how to create a object in javascript 
Javascript :: angular http async false 
Javascript :: template literal syntax 
Javascript :: how to skip the execution or for loop using continue js 
Javascript :: javascript array sorting 
Javascript :: node localstorage 
Javascript :: jquery bootstrap checkbox val 
Javascript :: How to iterate elements in an object 
Javascript :: row smaller than the container bootstrap react 
Javascript :: moves zeroes 
Javascript :: dark mode with react hooks 
Javascript :: stop python script nodejs 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =