Skip to content

Instantly share code, notes, and snippets.

@rafcontreras
Created September 8, 2020 01:29
Show Gist options
  • Save rafcontreras/fb81f942ceaac07cf019e8a0699ba423 to your computer and use it in GitHub Desktop.
Save rafcontreras/fb81f942ceaac07cf019e8a0699ba423 to your computer and use it in GitHub Desktop.
Javascript Map that waits for previous promise before starting next.
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