Skip to content

Instantly share code, notes, and snippets.

@acfatah
Last active March 17, 2023 02:38
Show Gist options
  • Save acfatah/a793cffa4faa861741e8f50f4df3bad7 to your computer and use it in GitHub Desktop.
Save acfatah/a793cffa4faa861741e8f50f4df3bad7 to your computer and use it in GitHub Desktop.
Javascript ES Module - sleep.js
/**
* Sleep, delay, pause or wait in Javascript
* @link https://www.sitepoint.com/delay-sleep-pause-wait/
* @param {Number} timeout Number in miliseconds
* @return {Promise}
*/
export const sleep = timeout => new Promise(resolve => setTimeout(resolve, timeout))
import { describe, it, expect } from 'vitest'
import { sleep } from './sleep'
describe('sleep', () => {
it('should wait for specified time', async () => {
const duration = 1000
const startTime = Date.now()
await sleep(1000)
const endTime = Date.now()
expect(endTime - startTime).toBeGreaterThanOrEqual(duration)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment