Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在这里,我如何修复nodeJS中的并发请求问题?_Javascript_Node.js_Es6 Promise - Fatal编程技术网

Javascript 在这里,我如何修复nodeJS中的并发请求问题?

Javascript 在这里,我如何修复nodeJS中的并发请求问题?,javascript,node.js,es6-promise,Javascript,Node.js,Es6 Promise,在节点课程中,讲师实现了一个名为promisequeue的类,以一次处理特定数量的请求,下面是该类 class PromiseQueue{ constructor(promises = [], num = 1){ this.concurrent = num; this.total = promises.length; this.todo = promises; this.running

在节点课程中,讲师实现了一个名为promisequeue的类,以一次处理特定数量的请求,下面是该类

class PromiseQueue{
        constructor(promises = [], num = 1){
            this.concurrent = num;
            this.total = promises.length;
            this.todo = promises;
            this.running = [];
            this.complete = [];
        }
        graphTasks(){
            var {todo , running, complete} = this;
            logUpdate(`
            
                todo=${todo.map(toX)}
                running=${running.map(toX)}
                complete=${complete.map(toX)}
            `)
        }
        get runAnother(){
            return this.running.length < this.concurrent && this.todo.length > 0
        }
        run(){
            while(this.runAnother){
                const promise = this.todo.shift();
                this.running.push(promise)
                promise.then(() => {
                    this.complete.push(this.running.shift())
                    this.graphTasks();
                    this.run();
                })
                this.graphTasks()
            }
        }
    }
这将为此添加第一个承诺。完成,尽管尚未解决 这是一个错误还是我错过了什么?我已经弄明白了。 如果第二个承诺解决了第一个问题,则第一个承诺将在仍运行时执行this.complete,第三个承诺将添加到this.running。 因此,我们还有一个在这个.complete中运行,还有一个在这个.running中运行,这保证了我们一次只有两个。 感谢所有试图解决这个问题的人

this.complete.push(this.running.shift())