Coder Social home page Coder Social logo

promise-pool's Introduction

promise-pool

简体中文版说明

What do you do when you have a set of async tasks to deal with?

//use for? If async tasks take a long time, they are too inefficient
for(let task of tasks){
    await task();
}

or

//use Promise.all,all tasks are executed together?
//it may be a good idea,but when you have a task that takes up a lot of resources, all the tasks are executed at the same time, there may be some surprises
await Promise.all(tasks.map(task => task()))

now,You can limit the amount of Promise thread count through 'promise-pool'!

let pool = new Thread({threadCount:10});

for(let task of tasks){
    pool.run(()=>task())
}
await pool.finish();

//or

await Promise.all(tasks.map(task => {
    return  pool.run(()=>task())
}))

Install

yarn add @meislzhua/promise-pool

or

npm i @meislzhua/promise-pool --save

Usage

//Any of the import ways can be used
//import Thread from "@meislzhua/promise-pool"
//import {Thread}from "@meislzhua/promise-pool"

let {Thread} = require("@meislzhua/promise-pool");

(async ()=>{
    //Create a new thread pool that can only run five tasks at once
    let pool = new Thread({threadCount:5});
    

    //If you have batch tasks to deal with
    for(let i = 0; i < 10; i++){
        pool.run(async ()=>{
            //your task ,doSomeThing
            
            //delay 1 second
            await new Promise(resolve => setTimeout(resolve,1000))
            console.log("finish!");
            return Math.random()
        }).then(data=>{
            //data is your task return
            console.log(data) //Math.random()

        }).catch(error =>{
            //If your task is likely to report errors, you need to catch
        })
    }   

    //Gets the number of remaining tasks(This includes what is being executed and what is waiting to be executed)
    console.log(pool.count());
    
    //You can call the Finish function when you need to wait for all tasks to complete
    await pool.finish()
})()

Other

This project is written with 'Typescript', and the compiled JS version is' esnext '

If you have a compatible version, go to the 'node_modules/@meislzhua/promise-pool' directory and modify 'tsconifg.json' and recompile

promise-pool's People

Contributors

meislzhua avatar

Stargazers

 avatar

Watchers

James Cloos avatar  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.