Promise generators
mateogianolio opened this issue · 1 comments
mateogianolio commented
Hey, continuing on the generator trail today we'll write a promise generator.
Let's create a function that will return a 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.
trusktr commented
Cool!