Skip to content

Instantly share code, notes, and snippets.

@korczis
Last active August 22, 2017 20:05
Show Gist options
  • Save korczis/8507467 to your computer and use it in GitHub Desktop.
Save korczis/8507467 to your computer and use it in GitHub Desktop.
Deferred wrapper of cherio request
var deferred = require('deferred'),
request = require('request'),
cheerio = require('cheerio');
var deferredRequest = function(url) {
var d = deferred();
request(url, function (err, resp, body) {
if(err) {
d.reject(new Error("Unable to fetch '" + url + "', reason: " + err));
return;
}
if(resp.statusCode !== 200) {
d.reject(new Error("Unable to fetch '" + url + "', code: " + resp.statusCode));
return;
}
d.resolve(body);
});
return d.promise();
};
////////////////////
// Example usage
////////////////////
// Load URL
deferredRequest("http://www.firmy.cz/a").then(function(data) {
// THIS BLOCK IS DEFERRED HANDLER OF HTML DATA
// Load HTML to cheerio
var $ = cheerio.load(data);
// Prepare results array
var result = [];
// Iterate all resultsl
$('ul.abc > li > a').each(function (element) {
// Push them to result array
result.push({
href: this.attr("href"),
text: this.text()
});
});
// Create immediately resolved promise and resolve it with result
return deferred(result);
}).done(function(res) {
// THIS BLOCK IS NEXT HANDLER (see previous block)
console.log(JSON.stringify(res, null, 4));
}, function(err) {
console.log("ERROR: " + err);
});
@davesnx
Copy link

davesnx commented Feb 23, 2016

What's the difference between using defer or using Promises directly?

For example:

function scrapp (data) {
  const $ = cheerio.load(data)
  let promises = []
  return new Promise((resolve, reject) => {
    $('a').each((i, el) => {
      const text = el.attribs.title
      const href = el.attribs.href
      promises.push({text, href})
    })
  resolve(promises)
  })
}

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