Created
June 7, 2017 13:20
-
-
Save kettanaito/4beeeeaebbf09d66ee82b7064a700c4e to your computer and use it in GitHub Desktop.
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 statusCodes = { | |
success: 200, | |
error: 400, | |
}; | |
function all(promises) { | |
const transformedPromises = promises.map(promise => promise.then((result) => { | |
if (result.payload.statusCode !== statusCodes.success) { | |
throw new Error('PROMISE RESOLVED TO FAILURE! SHOULD FAIL THIS CHAIN TO SHREDS'); | |
} | |
})); | |
return Promise.all(transformedPromises); | |
} | |
/* dummy promises */ | |
var promise1 = new Promise(function(resolve, reject) { | |
resolve({ payload: { | |
statusCode: 200, | |
test: 'right', | |
}}); | |
}); | |
var promise2 = new Promise(function(resolve, reject) { | |
resolve({ payload: { | |
statusCode: 201, | |
test: 'right', | |
}}); | |
}); | |
all([promise1, promise2]).then(() => { | |
console.log('All ok'); | |
}).catch(() => { | |
console.log('Error 2'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment