Created
September 8, 2020 01:29
-
-
Save rafcontreras/fb81f942ceaac07cf019e8a0699ba423 to your computer and use it in GitHub Desktop.
Javascript Map that waits for previous promise before starting next.
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 getStuffInOrder = (array) => { | |
const results = []; | |
const failed = []; | |
array | |
.reduce( | |
(chain, item) => | |
chain.then(() => | |
promiseFunction(item) | |
.then((data) => { | |
if (data) { | |
return results.push(data); | |
} | |
return failed.push(item); | |
}) | |
.catch(() => failed.push(item)) | |
), | |
Promise.resolve([]) | |
) | |
.finally(() => { | |
// do something with results | |
// do something with failed | |
}) | |
.catch((error) => { | |
console.log(error); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment