Skip to content
New issue

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

Promise generators #7

Open
mateogianolio opened this issue Feb 19, 2016 · 1 comment
Open

Promise generators #7

mateogianolio opened this issue Feb 19, 2016 · 1 comment

Comments

@mateogianolio
Copy link
Member

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
Copy link

trusktr commented Aug 30, 2017

Cool!

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

No branches or pull requests

2 participants