Skip to content

Instantly share code, notes, and snippets.

@raulingg
Created November 22, 2019 17:25
Show Gist options
  • Save raulingg/9e18b8265f2b60d0a7906b7bf557741f to your computer and use it in GitHub Desktop.
Save raulingg/9e18b8265f2b60d0a7906b7bf557741f to your computer and use it in GitHub Desktop.
resolve promise sequentially by reduce
[...cohortSlugs].reduce((prevPromise, slug) => {
let applications;
return prevPromise
.then((acc) => {
applications = acc;
return Cohort.findOne({ slug });
})
.then(({ _id: cohort }) => [
...applications,
...applicationsFromFirestore
.filter(({ cohortId }) => cohortId === slug)
.map(({ cohortId, ...rest }) => ({ cohort, ...rest })),
]);
}, Promise.resolve([]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment