Skip to content

Instantly share code, notes, and snippets.

@zjutszl
Last active January 24, 2019 09:41
Show Gist options
  • Save zjutszl/af4132df508c9cdeaae32cbaa395919c to your computer and use it in GitHub Desktop.
Save zjutszl/af4132df508c9cdeaae32cbaa395919c to your computer and use it in GitHub Desktop.
[限制并发量] #nodejs #async #并发
const async = require('async')
/*
async.mapLimit(list, 最高并发数, item调用的函数,callback) {}
list: 一个数组,可以是url数组,也可以是对象数组。
* url数组 => 并发请求
* 对象数组 => 并发一些异步操作 // 在第三个参数中你能拿到数组的元素的一些值
最高并发数
item调用的函数
*/
// 并发url请求
const urls = ['url1', 'url2', ...]
async.mapLimit(urls, 10, async function(url) {
return request(url)
}, (err, results) => {
if (err) throw err
// results is now an array of the response bodies
console.log(results)
})
// 并发SDK操作 或 其他异步操作
const list = [{foo:1, bar:2}, {foo:2, bar:3} , ...]
async.mapLimit(list, 10, async function(item) {
doSth(item.foo, item.bar)
}, errHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment