Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.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 异步/等待函数的async.queue模拟_Javascript_Asynchronous_Async Await_Ecmascript 2017 - Fatal编程技术网

Javascript 异步/等待函数的async.queue模拟

Javascript 异步/等待函数的async.queue模拟,javascript,asynchronous,async-await,ecmascript-2017,Javascript,Asynchronous,Async Await,Ecmascript 2017,我正在更新一些代码。它有一个要加载数据库的部分,实现为: var customerQueue = async.queue(insertCustomer, DATABASE_PARALLELISM); customerQueue.drain = function() { logger.info('all customers loaded'); airportCodeMappingQueue.push(airportCodeMappings); } 用于编写回调函数的函数inser

我正在更新一些代码。它有一个要加载数据库的部分,实现为:

var customerQueue = async.queue(insertCustomer, DATABASE_PARALLELISM);
customerQueue.drain = function() {
    logger.info('all customers loaded');
    airportCodeMappingQueue.push(airportCodeMappings);
}
用于编写回调函数的函数
insertCustomer
。作为代码现代化的一部分,我将其更改为
async
/
wait

现在,假设我编写了一个等效的async.queue,如下所示:

let customerQueueElements = [];
var customerQueue = {};
customerQueue.push = (customers) => {
  customers.forEach(customer => {
    customerQueueElements.push(insertCustomer(customer))
  });
}

const processQueue = async (queue, parallelism) => {
  for (let i = 0; i < queue.length; i += parallelism) {
    for (let j = 0; j < parallelism; j++) {
      let q = []
      if (queue[i + j]) {
        q.push(queue[i + j])
      }
      await Promise.all(q)
    }
  }
}
让customerQueueElements=[];
var customerQueue={};
customerQueue.push=(客户)=>{
customers.forEach(customer=>{
customerQueueElements.push(插入客户(customer))
});
}
const processQueue=async(队列,并行性)=>{
for(设i=0;i
我现在可以执行
等待ProcessQueue(customerQueue,DATABASE_PARALLELISM)
,但语法不好,我为每个队列保留了一个可见的命名变量

处理这个问题的好方法是什么


另外,
drain()
应该连接到
然后
,对吗?

@Bergi的方向是正确的。我制定了一个过程中工作版本:

module.exports = function () {
  module.internalQueue = []
  module.func = undefined
  module.parallelism =  1

  const process = async () => {
    for (let i = 0; i < module.internalQueue.length; i += module.parallelism) {
      for (let j = 0; j < module.parallelism; j++) {
        let q = []
        if (module.internalQueue[i + j]) {
          q.push(module.func(module.internalQueue[i + j]))
        }
        await Promise.all(q)
      }
    }
    module.internalQueue = []
    module.drain()
  }

  module.queue = (func, parallelism = 1) => {
    module.func = func
    module.parallelism = parallelism
    return module
  }

  module.push = async (customers) => {
    module.internalQueue.push(customers)
    await process()
  }

  module.drain = () => {}

  return module
}
module.exports=函数(){
module.internalQueue=[]
module.func=未定义
module.parallelism=1
const进程=异步()=>{
for(设i=0;i{
module.func=func
module.parallelism=并行
返回模块
}
module.push=异步(客户)=>{
module.internalQueue.push(客户)
等待进程()
}
module.drain=()=>{}
返回模块
}
这还不完全正确。签名类似于
async
包,但我的版本在所有实例中共享队列

需要找到一种简单的方法,为每个
函数
创建一个单独的实例,例如“本地”队列。然后,它将基本上像原来的一样工作


将随着我的进展而更新。

您编写此文件的方式将基本上同步运行您的
processQueue
,即使它使用
async
promissions执行此操作。
Promise.all(q)
将正确执行,但是
for()
的每次迭代都将暂停,直到
Promise.all(q)
返回。这就是你想要的吗?@Deryck同步!=顺序。“语法不好,我为每个队列保留了一个可见的命名变量”-您应该能够通过将整个内容包装在一个返回(导出)的(模块工厂)函数中来解决这个问题只有一件事你需要使用队列。上下文只是为了吸引他的注意力,以防他认为循环会毫不延迟地进行,就像人们在获取旧代码并转换为新的
async/await
时一样。JavaScript只有一个线程。因此,没有什么是真正并行的。如果我了解
async.queue
的工作原理,我相信这件事会产生
并行性
异步请求的数量,就像最初的一样。一旦这些操作完成,它将再次运行此操作。您熟悉
async
软件包吗?我错了吗?