javascript伪多线程代码
2022/10/25    
    function runTask(taskArr,processAsyncFun,maxRun){
        return new Promise((resolve,reject)=>{
            if (taskArr.length===0) {
                resolve('over');
                return;
            }
            let taskArrOrg = JSON.parse(JSON.stringify(taskArr));
            taskArr = taskArr.reverse();
            maxRun = Math.min.apply('',[maxRun,taskArr.length]);
            let count=0;
            let runTaskEach = async function(){
                if(taskArr.length > 0){
                    taskCurr = taskArr.splice(-1)[0];
                    try{
                        await processAsyncFun(taskCurr,taskArrOrg.length-taskArr.length-1,taskArrOrg);
                    } catch(e) {
                        //console.log(e)   
                    }
                    if(taskArr.length===0){
                        count++;
                        if (count===maxRun) {
                            resolve('over')
                        }
                    }else{
                        try{
                            await runTaskEach();
                        }catch(e){
   
                        }
                    }
                }
            }
            for(let i=0;i<maxRun;i++){
                runTaskEach(taskArr);
            }
       
        })
    }


下一篇: