简体   繁体   English

如何在循环中具有同步和异步延迟承诺

[英]How to have a sync and async delayed promise inside a loop

I am using the following library to have some delay in a promise: 我正在使用以下库来延迟承诺:

const prom = require('util').promisify;
const delayedProm = prom(setTimeout);

i have two nested loops inside '.then(())', and in the inner loop there is if-condition. 我在'.then(())'内部有两个嵌套循环,并且在内部循环中有if条件。 when this if-condition is satisfied i want to delay to certain amount of time, then the loop should continue normally. 当满足此if条件时,我想延迟一定的时间,则循环应正常继续。 the promise should be resolved and returned when the two loops finish iterations. 当两个循环完成迭代时,应解决并返回诺言。

please let me know how to achieve that sync and async 请让我知道如何实现同步和异步

code : 代码

return func()
.then((execs) => {

for () {
    for () {

        if (condition) {
            dely(interval)
        }
    }
}

return resolvedPromise
})

To use delay that in a loop, await is very useful: 要在循环中使用delayawait非常有用:

return (async function() {
  const execs = await func();

  for () {
    for () {
      if (condition) {
        await delay(interval)
      }
    }
  }
})()

Read on: 继续阅读:

Using async/await with a forEach loop 在forEach循环中使用异步/等待

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM