casualjavascript / blog

A collection of javascript articles stored as Github issues.
https://casualjavascript.com
MIT License
34 stars 1 forks source link

Promise generators #7

Open mateogianolio opened 8 years ago

mateogianolio commented 8 years ago

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 6 years ago

Cool!