惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
H
Help Net Security
V
Visual Studio Blog
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
MyScale Blog
MyScale Blog
The Cloudflare Blog
Martin Fowler
Martin Fowler
D
Docker
腾讯CDC
F
Fortinet All Blogs
雷峰网
雷峰网
GbyAI
GbyAI
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
博客园 - 叶小钗

博客园 - howhy

TypeScript any vs unknown 详细对比 TypeScript interface vs type 完整对比 html 元素包含关系 this 指向 空对象 Object.keys for ...in Reflect.ownKeys ...区别 逻辑运算符和空值运算符 运算符优先级 js 类型显式转换 js 高级函数 js 方法重载 fetch timeout js 任务顺序执行 暂停 判断两个对象是否相同 js 动态拦截属性 js 通用动画 js groupby js 防抖和节流 实现 instanceof 操作符 js 单例模式 js 数组去重和扁平方法 js 继承方法 js new的过程实现 js deepCopy js '=='的隐性类型转换规则
js 并发任务
howhy · 2026-01-20 · via 博客园 - howhy
class TaskParallel{
    constructor(parallelcount=2){
        this.parallelcount=parallelcount;
        this.tasks=[];
        this.runningCount=0;
    }
    add(task){
        return new Promise((resolve,reject)=>{
            this.tasks.push({
                task,resolve,reject
            });
            this._runtask();
        })
    }
    _runtask(){
        while(this.runningCount<this.parallelcount && this.tasks.length>0){
            const {task,resolve,reject}=this.tasks.shift();
            this.runningCount++;
            task().then(resolve,reject).finally(()=>{
                this.runningCount--;
                this._runtask();
            })
        }

    }
}
function timeout(time){
    return new Promise((resolve)=>{
        setTimeout(()=>{resolve();},time)
    })
}
const superTask=new TaskParallel();
function addTask(time,name){
superTask.add(()=>timeout(time)).then(()=>console.log(`task ${name} finish`))
}

addTask(1000,'task1')
addTask(1000,'task11')
addTask(1000,'task21')
addTask(1000,'task31')