Skip to content

Commit

Permalink
Merge d43dcbd into 618303b
Browse files Browse the repository at this point in the history
  • Loading branch information
zzdjk6 committed Mar 22, 2020
2 parents 618303b + d43dcbd commit 953bd01
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 121 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ There are 2 ways to create an `AbortablePromise`:
#### Constructor

```typescript
const abortablePromise = new AbortablePromise<T>((resolve, resolve, abortSignal) => {
const abortablePromise = new AbortablePromise<T>((resolve, reject, abortSignal) => {
// ...
});
```
Expand All @@ -45,3 +45,37 @@ abortablePromise.abort();
// Abort with custom reason
abortablePromise.abort('I abort it');
```

## Receipes

### Use with fetch

```typescript
const loadData = (id: number) => {
retrun new AbortablePromise<Data>((resolve, reject, abortSignal) => {
fetch(url, { signal: abortSignal })
.then(response => response.json())
.then(parseJsonToData)
.then(resolve)
.catch(reject);
});
}

const abortablePromise = loadData(id);
abortablePromise.abort();
```

### Do something more when abort

```typescript
const abortablePromise = new AbortablePromise<Data>((resolve, reject, abortSignal) => {
abortSignal.addEventListener('abort', () => {
// Do something
});
// ...
});
```

## More

More background explain is available on my [blog](https://medium.com/@zzdjk6/a-simple-implementation-of-abortable-promise-using-abortcontroller-with-typescript-2c163ee050e8).

0 comments on commit 953bd01

Please sign in to comment.