Skip to content

Instantly share code, notes, and snippets.

@tresorama
Created January 21, 2023 20:09
Show Gist options
  • Save tresorama/5cf918c212724b3c71f695a541f9d08b to your computer and use it in GitHub Desktop.
Save tresorama/5cf918c212724b3c71f695a541f9d08b to your computer and use it in GitHub Desktop.
sleep
/**
* Generic setTimeout implementation with Promise, used for wait some time then do something.
* Generally used for simulating async operation execution time while developing.
* @param time Time to wait for, in ms.
* @param returnValue Optional. What will be returned from the promise.
* @returns Promise that always resolve with "returnValue" after "time" ms.
*/
export const sleep = <T,>(time: number, returnValue?: T) => new Promise<T | undefined>(resolve => setTimeout(() => resolve(returnValue), time));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment