Skip to content

Instantly share code, notes, and snippets.

@Flayed
Created April 18, 2018 15:26
Show Gist options
  • Save Flayed/c931338bdd337e2f197047b3a3536c8d to your computer and use it in GitHub Desktop.
Save Flayed/c931338bdd337e2f197047b3a3536c8d to your computer and use it in GitHub Desktop.
TypeScript async/await
// A long running thing
doTheThing(a: number, b: number): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
// Long running call (just pretend, ok?)
const c = a + b;
if (c > 0) {
resolve(true);
}
else {
resolve(false);
}
});
}
// Calling the long running thing with async/await
async doSomethingWithTheThing() {
const result = await this.doTheThing(1, 1);
if (result) {
alert('The thing worked!');
} else {
alert('The thing did not worked :(');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment