慕标琳琳
TALK IS CHEAP, SHOW ME THE CODEPromise versionconst array = []for(let i = 0; i < 109; ++i) array.push(i)const once = 100const times = Math.ceil
(array.length / once)const load = async url => ({ url }) // replace here with your load http.getconst init = async () => {
const results = [] for(let n = 0; n < times; ++n) {
const result = await Promise.all(array.slice(n * once, (n + 1) * once).map(url => load(url)))
results.push(result)
} return results
}
init().then(console.log).catch(console.error)async versionvar async = require('async')var array = [];// prepare datafor(var i = 0; i < 109; ++i) { // 模拟总共109个请求,可以任意更改
array.push(i)
}var once = 100;var times = Math.ceil(array.length / once);function parallelRequest(urls, cb) {// urls is an array, contains url
async.parallel(urls.map(url => load(url)), function(err, results) { if(err) return cb(err)
cb(null, results)
})
}function load(url) {
return function(callback) {
get(url, function(res) { // replace it using http.get
//.... focus on error and handle it
callback(null, url)
})
}
}var get = function(url, callback) {
callback(null, { url: 'url' + url
});
};// request 100 urls in parallel for each requestasync.times(times, function(n, next) {
parallelRequest(array.slice(n * once, (n + 1) * once), function(err, res) {
next(err, res);
});
}, function(err, res) { // we should now have times=2 array
console.log(res)
});