Created
April 18, 2018 15:26
-
-
Save Flayed/c931338bdd337e2f197047b3a3536c8d to your computer and use it in GitHub Desktop.
TypeScript async/await
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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