Last active
August 24, 2021 16:38
-
-
Save soyuka/2ecc583b310b0bbb9d66b73537ae4a77 to your computer and use it in GitHub Desktop.
Promise reflect API by bluebird to handle multiple errors in Promise.all
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
const Promise = require('bluebird') | |
const lambdas = require('./lambdas.js') | |
// Ref: https://stackoverflow.com/questions/25817366/bluebird-promise-all-multiple-promises-completed-aggregating-success-and-rejec | |
Promise.all(lambdas.map(function(promise) { | |
// On utilise l'API reflect pour inspecter a la main nos promesses voir http://bluebirdjs.com/docs/api/reflect.html | |
// grace a ca, la promesse réussira toujours | |
return promise.reflect(); | |
})).each(function(inspection) { | |
// Ici la lambda (ou promesse) a réussi | |
if (inspection.isFulfilled()) { | |
console.log("A promise in the array was fulfilled with", inspection.value()); | |
// Ici la lambda a échouée | |
} else { | |
console.error("A promise in the array was rejected with", inspection.reason()); | |
} | |
}) |
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
const Promise = require('bluebird') | |
const lambdas = require('./lambdas.js') | |
// Dans notre cas si on veut avoir les résultat en tableau et gérer une seule erreur: | |
Promise.all(lambdas.map(function(promise) { | |
return promise.reflect(); | |
})) | |
.then(function(inspections) { | |
const error = new Promise.AggregateError() | |
inspections.forEach((inspection) => { | |
if (inspection.isRejected()) { | |
error.push(inspection.reason()) | |
} | |
}) | |
if (error.length > 0) { | |
return Promise.reject(error) | |
} | |
return Promise.resolve(inspections.map(e => e.value())) | |
}) | |
// Ici si aucune erreur n'est apparue avec les données des resolutions | |
.then(function(data) {console.log('got data')}) | |
// Ici une erreur agrégée lorsque tout a fini | |
.catch(function(err) {console.error('got err', err)}) |
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
const Promise = require('bluebird') | |
const lambdas = require('./lambdas.js') | |
// On peut bien entendu créer un utilitaire settleAll avec ce comportement | |
function settleAll(promises) { | |
return Promise.all(promises.map((promise) => promise.reflect())) | |
.then(function(inspections) { | |
const error = new Promise.AggregateError() | |
inspections.forEach((inspection) => { | |
if (inspection.isRejected()) { | |
error.push(inspection.reason()) | |
} | |
}) | |
if (error.length > 0) { | |
return Promise.reject(error) | |
} | |
return Promise.resolve(inspections.map(e => e.value())) | |
}) | |
} | |
settleAll(lambdas) | |
.then((data) => {console.log('got data')}) | |
.catch((err) => {console.error('got err', err)}) |
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
const Promise = require('bluebird') | |
// simule 5 appels de lambdas qui echouent ou réussissent | |
const lambdas = [ | |
new Promise((resolve) => setTimeout(resolve, 100)), | |
Promise.resolve(), | |
Promise.reject(new Error('immediate error')), | |
new Promise((resolve, reject) => setTimeout(() => reject(new Error('delayed error')), 200)), | |
new Promise((resolve) => setTimeout(resolve, 500)), | |
] | |
module.exports = lambdas |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment