Last active
March 17, 2023 02:38
-
-
Save acfatah/a793cffa4faa861741e8f50f4df3bad7 to your computer and use it in GitHub Desktop.
Javascript ES Module - sleep.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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