Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/romagny13/ts-promise

TypeScript Promise
https://github.com/romagny13/ts-promise

promise typescript

Last synced: 12 days ago
JSON representation

TypeScript Promise

Awesome Lists containing this project

README

        

# TypeScript Promise

Light promise polyfill easy to understand.

[![Build Status](https://travis-ci.org/romagny13/ts-promise.svg?branch=master)](https://travis-ci.org/romagny13/ts-promise)

```
npm i romagny13-ts-promise -S
```

Support (documentation):
- Promise
- all (parallel)
- race

## With TypeScript

```js
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)

```js
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

```js
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

```html

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);
});

```

## IE Polyfill

```js
window.Promise = window.Promise || TSPromise;

var p1 = new Promise(function (resolve, reject) {
resolve('P1 resolved');
});

p1.then(function (result) {

}, function (reason) {

});
```