Created
June 1, 2015 16:50
-
-
Save nycdotnet/3aa4a1f0dc4bc8c06677 to your computer and use it in GitHub Desktop.
Asynchronous Promise-based exec demo
This file contains 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
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