Reputation: 683
How can i merge these 2 functions in 1? There are 2 api-s: /page1 and /page2. After that i want to merge the 2 arrays in one. I do this because github api shows only 100 objects per page in api.
request.get({
url: 'https://api.github.com/users/angular/repos?per_page=100&page=1',
json: true,
headers: {'User-Agent': 'request'}
}, (err, res, data) => {
if (err) {
console.log('Error:', err);
} else if (res.statusCode !== 200) {
console.log('Status:', res.statusCode);
} else {
console.log(data.length);
}
});
request.get({
url: 'https://api.github.com/users/angular/repos?per_page=100&page=2',
json: true,
headers: {'User-Agent': 'request'}
}, (err, res, data2) => {
if (err) {
console.log('Error:', err);
} else if (res.statusCode !== 200) {
console.log('Status:', res.statusCode);
} else {
});
console.log(data2.length);
}
});
Thank you!
Upvotes: 1
Views: 65
Reputation: 707328
I would use promises to track when both requests are done and then execute the two requests in parallel (which gets faster results):
const rp = require('request-promise');
Promise.all([
rp({
url: 'https://api.github.com/users/angular/repos?per_page=100&page=1',
json: true,
headers: {'User-Agent': 'request'}
}),
rp({
url: 'https://api.github.com/users/angular/repos?per_page=100&page=2',
json: true,
headers: {'User-Agent': 'request'}
})
])).then(results => {
let combined = results[0].concat(results[1]);
// process combined results here
}).catch(err => {
console.log(err);
});
Note that the request-promise
library (which is just a wrapper on the request
library) automatically checks for a 2xx status for you so you don't need that extra code.
Upvotes: 1
Reputation: 8351
You could call the second request after the first was finished, then use array.concat() method to merge both results:
request.get({
url: 'https://api.github.com/users/angular/repos?per_page=100&page=1',
json: true,
headers: {'User-Agent': 'request'}
}, (err, res, data1) => {
if (err) return console.log('Error:', err);
if (res.statusCode !== 200) return console.log('Status:', res.statusCode);
request.get({
url: 'https://api.github.com/users/angular/repos?per_page=100&page=2',
json: true,
headers: {'User-Agent': 'request'}
}, (err, res, data2) => {
if (err) return console.log('Error:', err);
if (res.statusCode !== 200) return console.log('Status:', res.statusCode);
// merge 2 arrays:
var data = data1.concat(data2);
});
});
Upvotes: 0
Reputation: 334
You could try async. It has powerful flow control ability.
Following code is how to use it to achieve your need. And here is document of times
.
async.times(2, (n, next) => {
request.get({
url: 'https://api.github.com/users/angular/repos?per_page=100&page=' + n,
json: true,
headers: {'User-Agent': 'request'}
}, (err, res, data) => {
next(err, data)
})
}, (err, datas) => {
// here datas contains data of two request
// and you could handle err at one place
})
Hope it helps.
Upvotes: 0