We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hey, continuing on the generator trail today we'll write a promise generator.
Let's create a function that will return a Promise.
Promise
var request = require('request'); function get(url) { return new Promise(function (resolve, reject) { request(url, function (error, response, body) { if (error || response.statusCode !== 200) return reject(); resolve(body); }); }); }
OK, so how do we use it?
get('http://en.wikipedia.org').then(function (body) { // resolved }, function () { // rejected });
Now, if we want to visit multiple urls the code quickly gets messy as hell. The solution? Generators.
function requester(urls) { return (function* () { for (var url of urls) yield get(url); }()); }
Usage:
var requests = requester([ 'https://en.wikipedia.org', 'https://en.wikipedia.org/wiki/Web_scraping' ]); for (var page of requests) { page.then(function (body) { // resolved }, function () { // rejected }); }
Looking for a practical implementation? Check out domp, a web scraper I wrote using this technique.
The text was updated successfully, but these errors were encountered:
Cool!
Sorry, something went wrong.
No branches or pull requests
Hey, continuing on the generator trail today we'll write a promise generator.
Let's create a function that will return a
Promise
.OK, so how do we use it?
Now, if we want to visit multiple urls the code quickly gets messy as hell. The solution? Generators.
Usage:
Looking for a practical implementation? Check out domp, a web scraper I wrote using this technique.
The text was updated successfully, but these errors were encountered: