Last active
August 28, 2024 10:27
-
-
Save w33ble/e8c66f5d0c16be26dd9df700ef575ecc to your computer and use it in GitHub Desktop.
native promise mapSeries implementation
This file contains hidden or 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