Skip to content

Instantly share code, notes, and snippets.

@nycdotnet
Created June 1, 2015 16:50
Show Gist options
  • Save nycdotnet/3aa4a1f0dc4bc8c06677 to your computer and use it in GitHub Desktop.
Save nycdotnet/3aa4a1f0dc4bc8c06677 to your computer and use it in GitHub Desktop.
Asynchronous Promise-based exec demo
import {Promise as Promise} from 'es6-promise';
module exec {
function doExec(shouldSucceed: boolean) : Promise<number> {
return new Promise((resolve, reject) => {
doSubExec(shouldSucceed)
.then((result) => {
resolve(result);
},(error) => {
reject(error);
});
});
}
function doSubExec(shouldItSucceed: boolean) : Promise<number> {
return new Promise((resolve, reject) => {
if (shouldItSucceed) {
resolve(42);
} else {
reject(-1);
}
});
}
function printNumericResult(theNumber: number) {
console.log("Result was " + theNumber);
}
function printNumericErrorResult(theNumber: number) {
console.log("Error was " + theNumber);
}
function doItAsync(shouldSucceed: boolean) {
doExec(shouldSucceed).then((result) => {
printNumericResult(result);
}).catch((error) => {
printNumericErrorResult(error);
});
}
doItAsync(true);
doItAsync(false);
console.log("If this shows first, the doItAsync calls are off in asynchronous Promise land!");
// usual output, but the output order is not guaranteed:
// If this shows first, the doItAsync calls are off in asynchronous Promise land!
// Result was 42
// Error was -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment