Skip to content

Instantly share code, notes, and snippets.

@rightfold
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightfold/08633cccef0a763d39ce to your computer and use it in GitHub Desktop.
Save rightfold/08633cccef0a763d39ce to your computer and use it in GitHub Desktop.
fn sleep(timeout) {
return new Promise(fn(resolve) {
setTimeout(fn() { resolve(); }, timeout);
});
}
async fn greetAfter(name, timeout) {
await sleep(timeout); // suspends until promise returned by sleep is resolved
return greet(name);
}
async fn main() {
val message = await greetAfter('Suzy', 2000); // suspends until promise returned by greetAfter is resolved
console.log(message);
}
main(); // this returns immediately
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment