Skip to content

Instantly share code, notes, and snippets.

@kopasetik
Last active February 22, 2016 01:57
Show Gist options
  • Save kopasetik/0a9d24aad0c4f21c0ef7 to your computer and use it in GitHub Desktop.
Save kopasetik/0a9d24aad0c4f21c0ef7 to your computer and use it in GitHub Desktop.
Mapping across an async array response
'use strict'
const async = require('async')
const a = [0,1,2,3,4,5,6,7,8,9]
/*
async.mapLimit(a, 2, fancy, (err, result) => {
console.log(`err: ${err}, result: ${result}`)
})
*/
function fancy(value, cb) {
return setTimeout(() => {
console.log(value)
cb(null, value)
}, 500)
}
function fancyPromise(value) {
return new Promise((resolve, reject) => {
return setTimeout(() => {
return resolve(value * 2)
}, 500)
})
}
Promise.all(a.map(fancyPromise))
.then(results => {
console.log(results)
})
.catch(err => {
console.error(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment