Last active
September 22, 2022 07:49
-
-
Save w33ble/e8c66f5d0c16be26dd9df700ef575ecc to your computer and use it in GitHub Desktop.
native promise mapSeries implementation
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
function mapSeries(arr) { | |
if (!Array.isArray(arr)) throw new Error('mapSeries requires an Array'); | |
const length = arr.length; | |
const results = new Array(length); | |
arr.reduce((chain, item, i) => { | |
return chain.then(() => item).then(val => results[i] = val); | |
}, Promise.resolve()) | |
.then(() => results); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript version:
Usage