3

I have a scenario where I want to call the function d() after the execution of three functions a(), b(), c(), These three functions executes parallely.

setTimeout(function a(){ alert("Hello A"); a()}, 3000);
setTimeout(function b(){ alert("Hello B"); b()}, 3000);
setTimeout(function c(){ alert("Hello C"); c()}, 3000);

After getting all the functions executed I want the below function d() to get executed

function d(){
console.log('hello D')
}

Any help would be appreciated.

asked Feb 19, 2018 at 12:32
2

4 Answers 4

9

You can do this like

var promise1 = new Promise(function(resolve, reject) {
 setTimeout(function a(){ alert("Hello A"); resolve();}, 3000);
})
var promise2 = new Promise(function(resolve, reject) {
 setTimeout(function b(){ alert("Hello B"); resolve();}, 3000);
})
var promise3 = new Promise(function(resolve, reject) {
 setTimeout(function c(){ alert("Hello C"); resolve();}, 3000);
})
Promise.all([promise1, promise2, promise3]).then(function() {
 function d(){
 console.log('hello D') 
 }
 d(); 
});

Durga
15.6k2 gold badges31 silver badges55 bronze badges
answered Feb 19, 2018 at 12:49
Sign up to request clarification or add additional context in comments.

5 Comments

damn, same answer, 10 seconds apart
@JeremyThille beat you :D
Let's say that you provided the ES5 solution and I provided the ES6 one :)
Yeah!! true that
Have you checked, you are calling the same function inside?
3

You can use Promise.all to do that.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

const a = new Promise( (resolve, reject) => setTimeout( () => {
					console.log("a is finished");
					resolve()
				}, 3000) ),
	 b = new Promise( (resolve, reject) => setTimeout( () => {
					console.log("b is finished");
					resolve()
				}, 1000) ),
	 c = new Promise( (resolve, reject) => setTimeout( () => {
					console.log("c is finished");
					resolve()
				}, 2000) )
const d = () => console.log('hello D')
Promise.all( [a,b,c] ).then(d)

answered Feb 19, 2018 at 12:49

Comments

1

Just define a promising timer once:

 const timer = ms => new Promise(res => setTimeout(res, ms));

So you can do:

 const log = v => _ => console.log(v);
 Promise.all([
 timer(3000).then(log("a")),
 timer(3000).then(log("b"))
 ]).then(log("c"));
answered Feb 19, 2018 at 12:53

Comments

1

You probably need some global variable/object to save state of executed each function and check at the end if you can start d function. Example:

// you probably need some global variable/object
oCheckedFunctions = { a:false, b:false, c:false };
function d(){
	alert('hello D')
}
function a(){ 
	alert("Hello A"); 
	oCheckedFunctions.a = true;
	checkAndTryForD();
}
function b(){ 
	alert("Hello B"); 
	oCheckedFunctions.b = true;
	checkAndTryForD();
}
function c(){ 
	alert("Hello C"); 
	oCheckedFunctions.c = true;
	checkAndTryForD();
}
function checkAndTryForD() {
	if (oCheckedFunctions.a && oCheckedFunctions.b && oCheckedFunctions.c) {
		d();
	}
}
// your functions
setTimeout(a, 3000);
setTimeout(b, 3000);
setTimeout(c, 3000);

answered Feb 19, 2018 at 12:51

2 Comments

Granted, this works, but there are more elegant ways to do it.
... and async/await :) hackernoon.com/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.