Created
June 30, 2017 19:12
-
-
Save rainabba/21bf3b741c6f9857d741b69ba8ad78b1 to your computer and use it in GitHub Desktop.
Promise.all() demonstration with iterations for refactoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wait for ALL promises to return OR any to crash and then return the array of values to the caller of .all() | |
// // //Itteration 1 | |
// // function runParallelPromisesSum() { | |
// // //Our function returns its own promise with our value that needed both the promises to return first | |
// // return new Promise( ( resolveAll, rejectAll ) => { | |
// // let promise1 = new Promise( ( resolvePromise1, rejectPromise1 ) => { | |
// // console.log("resolving 1"); | |
// // resolvePromise1( 1 ); | |
// // }), | |
// // promise2 = new Promise( ( resolvePromise2, rejectPromise2 ) => { | |
// // console.log("resolving 2"); | |
// // resolvePromise2( 2 ); | |
// // }); | |
// // Promise.all( [ promise1, promise2 ] ) | |
// // .then( values => { | |
// // console.log("resolved 1+2") | |
// // console.dir( values ); | |
// // resolveAll( values[0] + values[1] ); | |
// // }) | |
// // .catch( err => { | |
// // console.dir( err ); | |
// // }); | |
// // }); | |
// // } | |
// // runParallelPromisesSum().then( sum => { console.log( sum ) } ); | |
// //Itteration 2 | |
// ( function(){ | |
// //Our function returns its own promise with our value that needed both the promises to return first | |
// return new Promise( ( resolveAll, rejectAll ) => { | |
// let promise1 = new Promise( ( resolvePromise1, rejectPromise1 ) => { | |
// console.log("resolving 1"); | |
// resolvePromise1( 1 ); | |
// }), | |
// promise2 = new Promise( ( resolvePromise2, rejectPromise2 ) => { | |
// console.log("resolving 2"); | |
// resolvePromise2( 2 ); | |
// }); | |
// Promise.all( [ promise1, promise2 ] ) | |
// .then( values => { | |
// console.log("resolved 1+2") | |
// console.dir( values ); | |
// resolveAll( values[0] + values[1] ); | |
// }) | |
// .catch( err => { | |
// console.dir( err ); | |
// }); | |
// }); | |
// })().then( sum => { console.log( sum ) } ); | |
//Itteration 3 - success | |
( function( promises ){ | |
return new Promise( ( resolve, reject ) => { | |
Promise.all( promises ) | |
.then( values => { | |
console.log("resolved all promises") | |
console.dir( values ); | |
resolve( values.reduce( (sum,value) => { return sum+value }) ); //Use Array.prototype.reduce() to sum the values in the array | |
}) | |
.catch( err => { | |
console.dir( err ); | |
throw err; | |
}); | |
}); | |
})([ | |
new Promise( ( resolve, reject ) => { | |
console.log("resolving 1"); | |
resolve( 1 ); | |
}), | |
new Promise( ( resolve, reject ) => { | |
console.log("resolving 2"); | |
resolve( 2 ); | |
}) | |
]).then( sum => { console.dir( { sum: sum } ) } ) | |
.then( () => { | |
//Itteration 3 - fail | |
( function( promises ){ | |
return new Promise( ( resolve, reject ) => { | |
Promise.all( promises ) | |
.then( values => { | |
console.log("resolved all promises") | |
console.dir( values ); | |
resolve( values.reduce( (sum,value) => { return sum+value }) ); //Use Array.prototype.reduce() to sum the values in the array | |
}) | |
.catch( err => { | |
console.dir( err ); | |
throw err; | |
}); | |
}); | |
})([ | |
new Promise( ( resolve, reject ) => { | |
console.log("resolving 1"); | |
resolve( 1 ); | |
}), | |
new Promise( ( resolve, reject ) => { | |
console.log("resolving 2"); | |
reject( 2 ); | |
}) | |
]).then( sum => { console.dir( { sum: sum } ) } ) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment