Skip to content

Today Dat Learned

All about Programming and Computer Science

  • Home
  • Contact

Tag: Promise

JavaScript: setTimeout with Promise

const waitXSeconds = (x) => {
  return new Promise((resolve, reject) => {
    setTimeout(()=>{
      resolve('Done!')
    }, x * 1000 )
  })
}

// Usage
async () => {
  // Wait for 5 seconds
  await waitXSeconds(5);      
  // Do something else 
  // ...
}
Dat Hoang Programming Leave a comment April 27, 2020August 2, 2020 1 Minute
Blog at WordPress.com.
  • Subscribe Subscribed
    • Today Dat Learned
    • Already have a WordPress.com account? Log in now.
    • Today Dat Learned
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar