Javascript execute function after Promise.all - javascript

I have a set of functions in a promise and after all of them have been executed I need to call a specific function that used the results obtained from each function. This is the code:
async function returnValues() {
var finalVal1;
function returnValue1() {
return new Promise((resolve, reject) => {
finalVal1 = {};
//do stuff
return finalVal1;
})
}
var finalVal2;
function returnValue2() {
return new Promise((resolve, reject) => {
finalVal2 = {};
//do stuff
return finalVal2;
})
}
var finalVal3;
function returnValue3() {
return new Promise((resolve, reject) => {
finalVal3 = {};
//do stuff
return finalVal3;
})
}
var finalVal4;
function returnValue4() {
return new Promise((resolve, reject) => {
finalVal4 = {};
//do stuff
return finalVal4;
})
}
let promise = await Promise.all([returnValue1(), returnValue2(), returnValue3(), returnValue4()]).then(myFunction(finalVal1, finalVal2, finalVal3, finalVal4));
}
function myFunction(finalVal1, finalVal2, finalVal3, finalVal4) {
console.log(finalVal1);
console.log(finalVal2);
console.log(finalVal3);
console.log(finalVal4);
}
The problem is that every returnVal() function is working perfectly fine, and the myFunction() work fine if I'm calling them separately, but when I'm using the Promise.all it never executes the myFunction() bit.
Where am I wrong?
Thank you very much

Try like this :
Simplified "function that returns a Promise" to a constant.
Promises do resolve()
The result of await Promise.all is an array of values, this array is passed to myFunction
async function returnValues() {
const returnValue1 = new Promise((resolve, reject) => {
let finalVal1 = {};
//do stuff
resolve(finalVal1);
});
const returnValue2 = new Promise((resolve, reject) => {
let finalVal2 = {};
//do stuff
resolve(finalVal2);
})
}
const values = await Promise.all([returnValue1, returnValue2])
myFunction(values);
}
function myFunction(values) {
console.log(values[0]);
console.log(values[1]);
}

Related

How to wait a Promise inside a forEach loop

I'm using some Promises to fetch some data and I got stuck with this problem on a project.
example1 = () => new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('foo1');
}, 3000);
});
example2 = () => new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('foo2');
}, 3000);
});
doStuff = () => {
const listExample = ['a','b','c'];
let s = "";
listExample.forEach((item,index) => {
console.log(item);
example1().then(() => {
console.log("First");
s = item;
});
example2().then(() => {
console.log("Second");
});
});
console.log("The End");
};
If I call the doStuff function on my code the result is not correct, the result I expected is shown below.
RESULT EXPECTED
a a
b First
c Second
The End b
First First
Second Second
First c
Second First
First Second
Second The End
At the end of the function no matter how I try, the variable s gets returned as "", I expected s to be "c".
It sounds like you want to wait for each Promise to resolve before initializing the next: you can do this by awaiting each of the Promises inside an async function (and you'll have to use a standard for loop to asynchronously iterate with await):
const example1 = () => new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('foo1');
}, 500);
});
const example2 = () => new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('foo2');
}, 500);
});
const doStuff = async () => {
const listExample = ['a','b','c'];
for (let i = 0; i < listExample.length; i++) {
console.log(listExample[i]);
await example1();
const s = listExample[i];
console.log("Fisrt");
await example2();
console.log("Second");
}
console.log("The End");
};
doStuff();
await is only syntax sugar for Promises - it's possible (just a lot harder to read at a glance) to re-write this without async/await:
const example1 = () => new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('foo1');
}, 500);
});
const example2 = () => new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('foo2');
}, 500);
});
const doStuff = () => {
const listExample = ['a','b','c'];
return listExample.reduce((lastPromise, item) => (
lastPromise
.then(() => console.log(item))
.then(example1)
.then(() => console.log("Fisrt"))
.then(example2)
.then(() => console.log('Second'))
), Promise.resolve())
.then(() => console.log("The End"));
};
doStuff();
If you want NOT to wait for each promise to finish before starting the next;
You can use Promise.all() to run something after all your promises have resolved;
example1 = () => new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('foo1');
}, 3000);
});
example2 = () => new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('foo2');
}, 3000);
});
doStuff = () => {
const listExample = ['a','b','c'];
let s = "";
let promises = []; // hold all the promises
listExample.forEach((item,index) => {
s = item; //moved
promises.push(example1() //add each promise to the array
.then(() => {
console.log(item); //moved
console.log("First");
}));
promises.push(example2() //add each promise to the array
.then(() => {
console.log("Second");
}));
});
Promise.all(promises) //wait for all the promises to finish (returns a promise)
.then(() => console.log("The End"));
return s;
};
doStuff();

Promise.All function invocation issue

I'm using Promise.all function to resolve the multiple promises concurrently. See the below code -
function check1() {
console.log('check 1 invoked')
return new Promise((resolve, reject) => {
setTimeout(()=> resolve('check1'), 4000);
})
}
function check2() {
console.log('check2 invoked')
return new Promise((resolve, reject) => {
setTimeout(()=> resolve('check2'), 4000);
})
}
var arr = [check1(), check2()];
Promise.all(arr)
.then((response) => console.log('response======>',response))
.catch((error) => console.error('error',error))
The problem with the above approach is that when I create the promise array, The respective functions gets invoked. I want to change the above code in the manner that two functions call only from the promise.all function.
Note - Need to store the promises functions in the array. Like I'm doing as var arr.
This should do it, do not make an array of promises but an array of functions, then map the functions to promises:
function check1() {
console.log('check 1 invoked')
return new Promise((resolve, reject) => {
setTimeout(()=> resolve('check1'), 4000);
})
}
function check2() {
console.log('check2 invoked')
return new Promise((resolve, reject) => {
setTimeout(()=> resolve('check2'), 4000);
})
}
//do not store the promises, store the funciton that creates a promise
var arr = [check1, check2];
Promise.all(
//map functions to promises
arr.map(fn=>fn())
)
.then((response) => console.log('response======>',response))
.catch((error) => console.error('error',error))
You can do in this way too -
var check1 = new Promise((resolve, reject) => {
setTimeout(()=> resolve('check1'), 4000);
});
var check2 = new Promise((resolve, reject) => {
setTimeout(()=> resolve('check2'), 4000);
});
Promise.all([check1, check2]).then(values => {
console.log(values);
}, reason => {
console.log(reason)
});

Promise return multiple values

I use the following code to return promise which is working OK.
The promise return the data value
run: () => {
return new Promise((resolve, reject) => {
....
}).then((data) => {
let loginApi = data[0]
let test = 1;
}).catch((err) => {
if (err.statusCode === 302) {
var data = url.parse(err.response.headers.location, true)
resolve(data )
}
})
});
I call it
module.run()
.then((data) => {
And I was able to get the data.
now I want to return also value test in the resolve, how should I do it?
I try to add it like this
resolve({data,test});
resolve([data,test]);
with call like
module.run()
.then({data,test}) => {
without success(test is empty), I read about spread but this is the only option?
I use ES6 with bluebird latest version
If you are using promise chain, in promise chain you have then->then->catch->... format. Always return Promise.resolve or Promise.reject. Promise.resolve will give success result for next then block and Promise.reject will go to next catch block.
var module = {
run: () => {
return new Promise((resolve, reject) => {
// ....
resolve('promise resolved')
}).then((data) => {
let loginApi = data[0]
let test = 1;
return Promise.resolve({data,test})
}).catch((err) => {
if (err.statusCode === 302) {
var data = url.parse(err.response.headers.location, true)
return Promise.resolve({data, test});
}
return Promise.reject(err);
})
}
};
module.run().then(({data, test}) => {
console.log(data, test);
})

How to execute functions sequentially using Node Js Promise?

I'm new to Node js Promise I'm not sure whether I'm using the Promise correctly or not so here is my code.
function print(){
first('first')
.then(second('second'))
.then(third('third'));
}
function first(a){
return new Promise((resolve, reject) => {
var res1 = function (){
resolve(a);
}
});
console.log(a);
}
function second(b){
return new Promise((resolve, reject) => {
var res1 = function (){
resolve(b);
}
});
setTimeout(() => {
console.log(b);
}, 2000);
}
function third(c){
return new Promise((resolve, reject) => {
var res1 = function (){
resolve(c);
}
});
console.log(c);
}
My desired output is
first
second
third
Instead what I get is
first
third
//after two seconds
second
I'm missing something but I can't figure it out please explain me
To get the expected behaviour, you need to resolve inside of the timeout (next to the console log). You also cannot pass arguments into promise chain functions since they need to accept the promise from the previous thennable.
A working snippet is below:
print();
function print(){
first('first')
.then(second)
.then(third);
}
function first(a){
return new Promise((resolve, reject) => {
console.log(a);
resolve(a);
});
}
function second(b){
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("second");
resolve(b);
}, 2000);
});
}
function third(c){
return new Promise((resolve, reject) => {
console.log("third");
resolve(c)
});
}

Promise.all is not displaying the data

I am new to promises and I want to return the data from classes as promises. I have two classes with two function in each class. What I am doing each function is returning a promise from a class, but I am unable to execute promise.all. Below is the code
Class1
class TestClass {
firstPromise() {
return new Promise((resolve, reject) => {
resolve('return first promise')
})
}
secondPromise() {
return new Promise((resolve, reject) => {
resolve('return second promise')
})
}
}
module.exports.TestClass = TestClass;
Class2
class TestClass1 {
firstPromise() {
return new Promise((resolve, reject) => {
resolve('return first promise')
})
}
secondPromise() {
return new Promise((resolve, reject) => {
resolve('return second promise')
})
}
}
module.exports.TestClass1 = TestClass1;
Main function
let data = req.body;
let test1Object;
let testObject;
let testParam;
let testParam1;
if (data.hasOwnProperty('param1')) {
test1Object = new test1.TestClass1();
test1Object.firstPromise().then((data)=>{
testParam1 = test1Object.secondPromise();
});
}
if (data.hasOwnProperty('param2')) {
testObject = new test.TestClass();
testObject.firstPromise().then((data)=>{
testParam = testObject.secondPromise()
});
}
Promise.all([testParam,testParam1]).then((data)=>{
console.log(data)
});
it displays
[ undefined, undefined ]
When you're executing Promise.all(), the promises are not yet resolved, so testParam and testParam1 are undefined. I think you should assign the first promise to testParam1:
testParam1 = test1Object.firstPromise().then(data => test1Object.secondPromise());
and similarly the second to testParam:
testParam = testObject.firstPromise().then(data => testObject.secondPromise());

Categories