Coder Social home page Coder Social logo

limit-promise's Introduction

limit promise

EN:
Run multiple promise-returning & async functions with limited concurrency

CN:
Promise执行数量控制,即"并发"控制(众所周知js是单线程,并不存在真正的并发,只是形象说明才这么称呼的)。

应用场景如:控制异步请求的数量。

Install (安装)

npm install limit-promise --save
or
yarn add limit-promise

Tip

EN:
Make sure that the function you want to execute is:

  • returns a Promise that matches the Promise criteria
  • async function
  • make sure Promise resolves or reject, otherwise it will "block" the call queue

If it doesn't meet the standard, please adapt yourself

CN:
确保需要执行的函数是:

  • 返回一个符合Promise标准的promise
  • async函数
  • 确保Promise会resolve或者reject,否则会"阻塞"住调用队列。

如果不符合标准请自行适配一下。

Usage(使用)

// request.js
// a request module, contain 'get' and 'post' function

// limitRequest.js
const LimitPromise = require('limit-promise')
const request = require('./request')
// request maximum limit 并发请求上限
const MAX = 5
// core controller 核心控制器
const limitP = new LimitPromise(MAX)

// 通过控制器包装get和post方法,实际上是将请求函数递交给控制器处理
// Wrapping the get and post methods with the controller,
// The request function is actually handed over to the controller for processing
function get (url, params) {
  return limitP.call(request.get, url, params)
}

function post (url, params) {
  return limitP.call(request.post, url, params)
}

module.exports = {get, post}

// example.js

// import new request module
const request = require('./limitRequest')
// mock apis
const apis = [
  {url: 'www.baidu.com'},
  {url: 'www.taobao.com'},
  {url: 'www.qq.com'},
  {url: 'www.jd.com'},
  {url: 'www.meituan.com'},
  {url: 'www.toutiao.com'},
  {url: 'www.weibo.com'},
  {url: 'www.zhihu.com'},
  {url: 'www.alipay.com'}
]

// start multiple network request
// Because the maximum number of requests is limited to 5, the first five requests will start immediately, and the sixth will start in the ready queue for execution
// Once a request is completed and returned, a task is removed from the queue for execution
// 简单粗暴的使用for循环启动数个请求
// 由于限制了最大请求数量是5,因此前五个请求将立即启动,而第6个开始将进入就绪队列等待执行
// 一旦某个请求结束并返回,将从队列中取下一个任务执行
for (let i = 0; i < apis.length; i++) {
  request.get(apis[i].url, {id: i})
    .then(res => console.log(`request resolve : ${res}`))
}

License

MIT

limit-promise's People

Contributors

leejialing avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.