Skip to content

Commit

Permalink
docs(peekPromiseState): replace promiseState to peekPromiseState
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Aug 20, 2024
1 parent c4d0431 commit 9ec7a6d
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,37 @@ assertEquals(await promiseState(waiter1), "fulfilled");
assertEquals(await promiseState(waiter2), "fulfilled");
```

### promiseState
### peekPromiseState

`promiseState` is used to determine the state of the promise. Mainly for testing
purpose.
`peekPromiseState` is used to determine the state of the promise. Mainly for
testing purpose.

```typescript
import { promiseState } from "@core/asyncutil/promise-state";
import { peekPromiseState } from "@core/asyncutil/peek-promise-state";

const p1 = Promise.resolve("Resolved promise");
console.log(await promiseState(p1)); // fulfilled
console.log(await peekPromiseState(p1)); // fulfilled

const p2 = Promise.reject("Rejected promise").catch(() => undefined);
console.log(await promiseState(p2)); // rejected
console.log(await peekPromiseState(p2)); // rejected

const p3 = new Promise(() => undefined);
console.log(await promiseState(p3)); // pending
console.log(await peekPromiseState(p3)); // pending
```

Use `flushPromises` to wait all pending promises to resolve.

```typescript
import { flushPromises } from "@core/asyncutil/flush-promises";
import { peekPromiseState } from "@core/asyncutil/peek-promise-state";

const p = Promise.resolve<void>(undefined)
.then(() => {})
.then(() => {});

console.log(await peekPromiseState(p)); // pending
await flushPromises();
console.log(await peekPromiseState(p)); // fulfilled
```

### Queue/Stack
Expand Down

0 comments on commit 9ec7a6d

Please sign in to comment.