Skip to content

Instantly share code, notes, and snippets.

@benjaminparnell
Last active December 5, 2022 23:14
Show Gist options
  • Select an option

  • Save benjaminparnell/e43ff69e06219221d0dc066d3ddcabb6 to your computer and use it in GitHub Desktop.

Select an option

Save benjaminparnell/e43ff69e06219221d0dc066d3ddcabb6 to your computer and use it in GitHub Desktop.
function map (arr, fn, ctx = this) {
return Promise.all(arr.map((item, index) => {
return new Promise((resolve, reject) => {
fn.call(ctx, item, index, arr, resolve, reject)
})
}))
}
var arr = [1, 2, 3]
map(arr, (item, index, arr, resolve, reject) => {
setTimeout(function () {
resolve(item * 2)
}, item * 500)
})
.then((values) => {
console.log(values)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment