Last active
August 22, 2017 20:05
-
-
Save korczis/8507467 to your computer and use it in GitHub Desktop.
Deferred wrapper of cherio request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the difference between using defer or using Promises directly?
For example: