Skip to content

Instantly share code, notes, and snippets.

@YuriFontella
Last active February 2, 2022 16:00
Show Gist options
  • Save YuriFontella/ec71059462c09cfab4fe79f19adfe49d to your computer and use it in GitHub Desktop.
Save YuriFontella/ec71059462c09cfab4fe79f19adfe49d to your computer and use it in GitHub Desktop.
(async () => {
console.time()
async function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
const time = 2000
const total = 2
const pause = 6000
const array = ['godzilla', 'homer', 'eminem', 'valorant', 'megan fox', 'yuri', 'stalin', 'graphql']
let counter = 0
await Promise.all(array.map(async (x, i) => {
if (i > 0) {
await delay((i + 1) * time)
}
console.log(x)
counter = counter + 1
if (counter === total) {
console.log('\npause\n')
counter = 0
await delay(pause)
}
}))
console.timeEnd()
})()
@YuriFontella
Copy link
Author

(async () => {

  console.time()

  async function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
  }

  const time = 2000
  const total = 2
  const pause = 6000

  const array = [{ name: 'godzilla' }, { name: 'homer' }, { name: 'eminem' }, { name: 'valorant' }, { name: 'megan fox' }, { name: 'yuri' }, { name: 'stalin' }, { name: 'graphql' }]

  let counter = 0

  async function execute(i, v) {

    if (i > 0) {
      await delay(time)
    }

    console.log(v.name)

    counter = counter + 1

    if (counter === total) {

      console.log('\npause\n')

      counter = 0

      await delay(pause)
    }
  }

  for (let [i, v] of array.entries()) {

    await execute(i, v)
  }

  console.timeEnd()
})()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment