romagny13-ts-promise
v1.0.4
Published
TypeScript Promise
Downloads
4
Maintainers
Readme
TypeScript Promise
Light promise polyfill easy to understand.
npm i romagny13-ts-promise -S
Support (documentation):
- Promise
- all (parallel)
- race
With TypeScript
import * as TSPromise from 'romagny13-ts-promise';
let p1 = new TSPromise((resolve, reject) => {
/* support :
- resolve
- reject
- throw exception
*/
setTimeout(() => {
resolve('P1 resolved');
}, 500);
});
p1.then((result) => {
/* support :
- return value
- throw exception
*/
}, (reason) => {
/* support :
- return value
- throw exception
*/
});
Chaining and ignore useless callbacks (example with all)
let p1 = new TSPromise((resolve, reject) => resolve('p1 resolved'));
let p2 = new TSPromise((resolve, reject) => resolve('p2 resolved'));
TSPromise.all([p1, p2]).then((result) => {
return 'return value';
}).catch(() => { })
.catch(() => { })
.catch(() => { })
.catch(() => { })
.then((result) => {
// ... result 'return value'
});
Or with an exception
let p1 = new TSPromise((resolve, reject) => resolve('p1 resolved'));
let p2 = new TSPromise((resolve, reject) => resolve('p2 resolved'));
TSPromise.all([p1, p2]).then((result) => {
throw 'my error';
}).catch((reason) => {
// ... reason 'my error'
});
es5
<script src="node_modules/romagny13-ts-promise/dist/ts-promise.js"></script>
<script>
var p1 = new TSPromise(function (resolve, reject) {
setTimeout(function () {
resolve('P1 resolved');
}, 500);
});
p1.then(function (result) {
console.log('completed:', result);
}, function (reason) {
console.log('error', reason);
});
</script>
IE Polyfill
window.Promise = window.Promise || TSPromise;
var p1 = new Promise(function (resolve, reject) {
resolve('P1 resolved');
});
p1.then(function (result) {
}, function (reason) {
});