Last active
February 22, 2016 01:57
-
-
Save kopasetik/0a9d24aad0c4f21c0ef7 to your computer and use it in GitHub Desktop.
Mapping across an async array response
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
'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