Skip to content

Instantly share code, notes, and snippets.

@antomor
Last active October 21, 2020 17:07
Show Gist options
  • Save antomor/70fd5c22254d2bbb0abc146406e0cebe to your computer and use it in GitHub Desktop.
Save antomor/70fd5c22254d2bbb0abc146406e0cebe to your computer and use it in GitHub Desktop.
It includes a wait function that returns a promise to be waited, it is also possible to specify a cancelToken, to stop waiting.
const wait = (milliSeconds) => {
return new Promise((resolve) => setTimeout(() => resolve(), milliSeconds));
};
wait(200).then(() => {
// your code
});
// async/await context
await wait(200);
//your code
const wait = (milliSeconds, cancelToken) => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), milliSeconds);
cancelToken.then(() => reject())
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment