简体   繁体   中英

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. when this if-condition is satisfied i want to delay to certain amount of time, then the loop should continue normally. 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:

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

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

Read on:

Using async/await with a forEach loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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