Skip to content

Promise generators #7

Description

@mateogianolio

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions