var array = [1,2,3,4];function check(){if(array.length > 0)var url = array.pop();
load(url);
}}function load(url){
http.get(url, function(res){
check();
)
}}做了个简单的队列 但现在 执行一个 再执行 一个 我想批量执行100个 再执行 100个 怎么控制呢 请问
1 回答
慕标琳琳
TA贡献1830条经验 获得超9个赞
TALK IS CHEAP, SHOW ME THE CODE
Promise version
const 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 version
var 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) });
添加回答
举报
0/150
提交
取消