Skip to content

Instantly share code, notes, and snippets.

@onosendi
Created January 4, 2022 15:33
Show Gist options
  • Save onosendi/2b3e0511098ca9534108f2c42a1acbe7 to your computer and use it in GitHub Desktop.
Save onosendi/2b3e0511098ca9534108f2c42a1acbe7 to your computer and use it in GitHub Desktop.
function sleep(miliseconds = 1000) {
return new Promise((resolve) => setTimeout(resolve, miliseconds));
}
// This will log 'foo' and 'bar' immediately.
async function foo1() {
console.log('foo');
sleep();
console.log('bar');
}
// This will log 'foo', and 'bar' won't be logged until `sleep` is done.
// It's saying 'wait for this, then continue'.
async function foo2() {
console.log('foo');
await sleep();
console.log('bar');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment