-
-
Save justsml/939cf1b2b668e69a13e29ef997ea6ddb to your computer and use it in GitHub Desktop.
Promise map (Bluebird)
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
const Bluebird = require('bluebird') | |
const got = require('got') | |
const urls = [ | |
"http://www.google.com/", | |
"http://www.yahoo.com/", | |
"http://www.bing.com/", | |
]; | |
// Bluebird is an extension of promises: | |
Bluebird.resolve(urls) | |
.map(url => got(url), {concurrency: 4}) | |
.map(response => response.body) | |
.map((html, i) => { | |
// Now you can access/parse each response as `html` here, and reshape the value for the next `.then` or `.map` | |
return {html: html, url: urls[i]} | |
}, {concurrency: 8}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment