I am learning promises in Node JS. I came across a certain code which I want to synchronize it (Out of Curiousity).
function test1(){
return new Promise(async function(resolve,reject){
await promise1();
await promise2();
await promise3();
console.log('last');
resolve(true);
})
}
async function promise1(){
return new Promise(async function(resolve,reject){
console.log('promise1');
await setTimeout(myFunc, 1500, resolve);
console.log("111");
});
}
function myFunc(resolve) {
console.log(`arg was => resolve`);
resolve(true);
}
function promise2(){
return new Promise(function(resolve,reject){
console.log('promise2');
resolve(true);
});
}
function promise3(){
return new Promise(function(resolve,reject){
console.log('promise3');
resolve(true);
});
}
function callTest1(){
for(i=0;i<4;i++){
test1();
}
}
callTest1();
I want the output like this :
promise1
arg was => resolve
111
promise2
promise3
last
promise1
arg was => resolve
111
promise2
promise3
last
promise1
arg was => resolve
111
promise2
promise3
last
promise1
arg was => resolve
111
promise2
promise3
last
I want my code to wait for setTimeOut and then after only it should run other function. I know, in node js we have concept of Asynchronization But somehow I want this code to be sync.