Skip to content

Instantly share code, notes, and snippets.

@gh640
Created November 21, 2023 05:51
Show Gist options
  • Save gh640/40f9eb5b5dc8fca3e3abf12fc804bedd to your computer and use it in GitHub Desktop.
Save gh640/40f9eb5b5dc8fca3e3abf12fc804bedd to your computer and use it in GitHub Desktop.
JavaScript: async `waitFor` function
/**
* Wait for the specified milliseconds.
*/
async function waitFor(msec) {
await new Promise((resolve) => setTimeout(resolve, msec));
}
@gh640
Copy link
Author

gh640 commented Mar 8, 2024

Different declaration with the similar behavior:

const waitFor = (msec) => new Promise((resolve) => setTimeout(resolve, msec));

@gh640
Copy link
Author

gh640 commented Mar 8, 2024

Usage:

// Wait 1000 milliseconds.
await waitFor(1000);

// Wait 3000 milliseconds and print out.
waitFor(3000).then(() => console.log("3 seconds."));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment