Last active
October 25, 2016 09:35
-
-
Save SamirTalwar/cdea07f38182887421ffc2e4fc5937fe to your computer and use it in GitHub Desktop.
Implementations of functions that work on Promise arrays, including `all` and `race`.
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 all = promises => { | |
if (promises.length === 0) | |
return Promise.resolve([]) | |
let [head, ...tail] = promises | |
return head.then(h => | |
all(tail).then(t => | |
[h].concat(t))) | |
} |
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 allSuccessful = promises => { | |
if (promises.length === 0) | |
return Promise.resolve([]) | |
let [head, ...tail] = promises | |
return head.then(h => | |
allSuccessful(tail) | |
.then( | |
t => [h].concat(t)), | |
() => allSuccessful(tail)) | |
} |
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 race = promises => | |
new Promise((resolve, reject) => | |
promises.forEach(promise => | |
promise.then(resolve, reject))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment