-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js-core):
sleep()
now accepts a unit of time
- Loading branch information
Showing
2 changed files
with
23 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
import { convertTime } from './time-utils'; | ||
|
||
/** | ||
* Returns a promise that resolves after `ms` milliseconds. | ||
* Returns a promise that resolves after a specified delay. | ||
* | ||
* @param unit Can be anything listed in the docs for {@link TimeUnit}. Defaults to milliseconds | ||
* | ||
* ```ts | ||
* // do something | ||
* await sleep(1000); // wait a second | ||
* await sleep(1_000); // wait 1 second | ||
This comment has been minimized.
Sorry, something went wrong. |
||
* // do something else | ||
* await sleep(2, 'hours'); // wait 2 hours | ||
* // do a final thing | ||
* ``` | ||
*/ | ||
export async function sleep(ms: number): Promise<undefined> { | ||
export async function sleep(delay: number, unit = 'ms'): Promise<undefined> { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
setTimeout(resolve, convertTime(delay, unit, 'ms')); | ||
}); | ||
} |
TIL about the numeric separator syntax. Cool!