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 :: rating 
Javascript :: basic area chart 
Javascript :: async await map 
Javascript :: asyncio.sleep in javascript 
Javascript :: fs.readfilesync withFileTypes true 
Javascript :: react lifecycle hooks 
Javascript :: callback in javascript 
Javascript :: ex:js 
Javascript :: add property with value in js 
Javascript :: convert json to dart 
Javascript :: add value to object 
Javascript :: timer javascript 
Javascript :: on hover event 
Javascript :: how to check if a number is negative in p5.js 
Javascript :: Everything Be True 
Javascript :: redux if already exist item dont add to array 
Javascript :: window.location.href breaks back button 
Javascript :: dart get vfirst key value of map 
Javascript :: code for random dom background image change 
Javascript :: angular print an array 
Javascript :: gdscript create node 
Javascript :: javascript get the first day of the month and last day 
Javascript :: js browse file 
Javascript :: angular routing example 
Javascript :: feathersjs quicstart 
Javascript :: how does a dictionary from c# translate into js 
Javascript :: using a variable in regex javascript with boundary marker 
Javascript :: vuex store example medium 
Javascript :: test 
Javascript :: ja display snippet from text 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =