UPDATE: I FOUND AN "HOMEMADE" SOLUTION BY MY OWN, SCROLL DOWN TO THIS THIS SOLUTION!
When I'm doing NodeJS promises "manually" I have the option to pass the resolve value from the parent promise to it's child, or more precisely, from the first promise to the second and so for and so on.
So, after first promise is done, I can continue using it's resolved data in the next promise. Sometimes this data is crucial for the whole process. Such as sending a SMS via remote API, if the SMS will not be sent, I cannot be able to manage the next function.
I have tried the "shortcut" (*sorry if it does not meant to be a shortcut): Promise.all. Well it's nice, and it pops up the catched promise reject value first and if everything is okay it goes to finish. But how can I preserve the resolved data in the same manner I have described above?
I will push here some code, so we can understand each other better:
Trying to do this using Promise.all
function test1() {
return new Promise(function(resolve, reject) {
resolve("test1");
});
}
function test2() {
return new Promise(function(resolve, reject) {
resolve("test2");
});
}
function test3() {
return new Promise(function(resolve, reject) {
resolve("test3");
});
}
Promise.all([test1, test2, test3].map(func => func())).then((result) => {
console.log('result: ', result);
}).catch((result) => {
console.log("Catched: ", result);
});
Trying to do this using the "manual" way:
function test1() {
return new Promise(function(resolve, reject) {
resolve("test1");
});
}
function test2(p) {
return new Promise(function(resolve, reject) {
resolve("test2");
});
}
function test3(p) {
return new Promise(function(resolve, reject) {
resolve("test3");
});
}
test1().then(function(result) {
return test2(result);
}).then(function(result) {
return test3();
}).then(function(result) {
console.log("Finished");
});
Now for the catch section if I am using the Promise.all function with the catch call back, it will rise the first catched match. This is not good. Since I want to first tell the user he forgot filling email, later he has to be notified whether there was a problem sending SMS. But it does not working like that.
Actually, I can do it the manual way seperate for then and seperate for catch, but it is a mess.. And it is very easy to cause mistake in the syntax that will take hours to find.
********************** UPDATE - I FOUND AN HOMEMADE SOLUTIN!!! **********************
function test1() {
// some process return in overall as boolean
var overall = true;
if (overall)
return test2({test1: "is resolved"});
return catched("rejected on 1");
}
function test2(result) {
// some process return in overall as boolean
var overall = true;
result.test2 = "is resolved";
if (overall)
return test3(result);
return catched("rejected on 2");
}
function test3(result) {
// some process return in overall as boolean
var overall = true;
result.test3 = "is resolved";
if (overall)
return finished(result);
return catched("rejected on 3");
}
function finished(result) {
console.log(result);
}
function catched(result) {
console.log(result);
}
test1();
test1() will run first. After each of them done it goes to the next, syntax is simple, my homemade Promise.all is super simple thats the solution. Plus, if in future we want to add a middle function between test2 to test3, we should not think about adding it in the end handle.
Hmm.. probably that is it for now. Hope it is clear to understand and help others!
THANKS :)