Last active
March 15, 2016 04:28
-
-
Save khanghoang/2c8149cc20bbbd1536e9 to your computer and use it in GitHub Desktop.
promise stuff
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 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 + '§ion=&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