Skip to content

Instantly share code, notes, and snippets.

@khanghoang
Last active March 15, 2016 04:28
Show Gist options
  • Save khanghoang/2c8149cc20bbbd1536e9 to your computer and use it in GitHub Desktop.
Save khanghoang/2c8149cc20bbbd1536e9 to your computer and use it in GitHub Desktop.
promise stuff
var Q = require('q');
var request = require('request');
var Promise = require('bluebird')
var numberOfArticle = 0;
function getArticleUrlPerPage(pageNumber, articleUrls) {
var options = {
method: 'GET',
url: 'https://www.getnewsmart.com/?last_time_title=This+week&page=' + pageNumber + '&section=&xhr=true',
};
return new Promise(function(resolve, reject) {
request(options, function(error, response, body) {
if (error) {
reject(new Error(error));
return;
}
resolve(response);
});
})
}
var articleUrls = [];
function getArticleUrlWholeSite() {
console.log('Starting to get article urls');
var arrayPromises = [];
for (var pageNumber = 1; pageNumber <= 3; pageNumber++) {
arrayPromises.push(getArticleUrlPerPage(pageNumber, articleUrls));
}
return arrayPromises;
}
Q.all(getArticleUrlWholeSite())
.then(function () {
console.log('End getArticleUrlWholeSite');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment