Skip to content

Instantly share code, notes, and snippets.

@Flayed
Created April 18, 2018 15:22
Show Gist options
  • Save Flayed/2efb8682c01e5471ccb2fbe318c7b9f4 to your computer and use it in GitHub Desktop.
Save Flayed/2efb8682c01e5471ccb2fbe318c7b9f4 to your computer and use it in GitHub Desktop.
TypeScript promise
// 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
doSomethingWithTheThing() {
this.doTheThing(1, 1).then((result) => {
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