Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Created December 16, 2016 15:26
Show Gist options
  • Star 58 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save daliborgogic/7ee40bcff586ae08b33bf929172d61e8 to your computer and use it in GitHub Desktop.
Save daliborgogic/7ee40bcff586ae08b33bf929172d61e8 to your computer and use it in GitHub Desktop.
Node.js Async/Await delay
'use strict'
const timeout = ms => new Promise(res => setTimeout(res, ms))
function convinceMe (convince) {
let unixTime = Math.round(+new Date() / 1000)
console.log(`Delay ${convince} at ${unixTime}`)
}
async function delay () {
convinceMe('started')
await timeout(5000)
convinceMe('finished')
}
delay()
@teebu
Copy link

teebu commented Jul 21, 2018

'use strict'

const timeout = ms => new Promise(res => setTimeout(res, ms))

function convinceMe (convince) {
  let unixTime = new Date().getTime()
  console.log(`Delay ${convince} at ${unixTime}`)
  return unixTime  
}

async function delay () {
  let start = convinceMe('started')
  await timeout(5501)
  let end = convinceMe('finished')
  console.log('Diff:', end - start)
}

delay()

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