-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from jsr-core/improve-promise-state
feat: add `flushPromises` and `peekPromiseState` then deprecate `promiseState`
- Loading branch information
Showing
13 changed files
with
309 additions
and
69 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Flush all pending promises in the microtask queue. | ||
* | ||
* ```ts | ||
* import { flushPromises } from "@core/asyncutil/flush-promises"; | ||
* | ||
* let count = 0; | ||
* Array.from({ length: 5 }).forEach(() => { | ||
* Promise.resolve() | ||
* .then(() => count++) | ||
* .then(() => count++); | ||
* }); | ||
* | ||
* console.log(count); // 0 | ||
* await flushPromises(); | ||
* console.log(count); // 10 | ||
* ``` | ||
* | ||
* The original idea comes from [flush-promises] package in npm. | ||
* | ||
* [flush-promises]: https://www.npmjs.com/package/flush-promises | ||
*/ | ||
export function flushPromises(): Promise<void> { | ||
return new Promise((resolve) => setTimeout(resolve)); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { test } from "@cross/test"; | ||
import { assertEquals } from "@std/assert"; | ||
import { flushPromises } from "./flush_promises.ts"; | ||
|
||
test( | ||
"flushPromises() flushes all pending promises in the microtask queue", | ||
async () => { | ||
let count = 0; | ||
Array.from({ length: 5 }).forEach(() => { | ||
Promise.resolve() | ||
.then(() => count++) | ||
.then(() => count++); | ||
}); | ||
assertEquals(count, 0); | ||
await flushPromises(); | ||
assertEquals(count, 10); | ||
}, | ||
); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const t = Symbol("pending mark"); | ||
|
||
/** | ||
* Promise state | ||
*/ | ||
export type PromiseState = "fulfilled" | "rejected" | "pending"; | ||
|
||
/** | ||
* Peek the current state (fulfilled, rejected, or pending) of the promise. | ||
* | ||
* ```ts | ||
* import { assertEquals } from "@std/assert"; | ||
* import { peekPromiseState } from "@core/asyncutil/peek-promise-state"; | ||
* | ||
* assertEquals(await peekPromiseState(Promise.resolve("value")), "fulfilled"); | ||
* assertEquals(await peekPromiseState(Promise.reject("error")), "rejected"); | ||
* assertEquals(await peekPromiseState(new Promise(() => {})), "pending"); | ||
* ``` | ||
* | ||
* Use {@linkcode https://jsr.io/@core/asyncutil/doc/flush-promises/~/flushPromises flushPromises} | ||
* to wait for all pending promises to be resolved prior to calling this function. | ||
* | ||
* ```ts | ||
* import { assertEquals } from "@std/assert"; | ||
* import { flushPromises } from "@core/asyncutil/flush-promises"; | ||
* import { peekPromiseState } from "@core/asyncutil/peek-promise-state"; | ||
* | ||
* const p = Promise.resolve<void>(undefined) | ||
* .then(() => {}) | ||
* .then(() => {}); | ||
* assertEquals(await peekPromiseState(p), "pending"); | ||
* await flushPromises(); | ||
* assertEquals(await peekPromiseState(p), "fulfilled"); | ||
* ``` | ||
*/ | ||
export function peekPromiseState(p: Promise<unknown>): Promise<PromiseState> { | ||
return Promise.race([p, t]).then( | ||
(v) => (v === t ? "pending" : "fulfilled"), | ||
() => "rejected", | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { peekPromiseState } from "./peek_promise_state.ts"; | ||
|
||
Deno.bench({ | ||
name: "current", | ||
fn: async () => { | ||
await peekPromiseState(Promise.resolve("fulfilled")); | ||
}, | ||
group: "peekPromiseState (fulfilled)", | ||
baseline: true, | ||
}); | ||
|
||
Deno.bench({ | ||
name: "current", | ||
fn: async () => { | ||
const p = Promise.reject("reject").catch(() => {}); | ||
await peekPromiseState(p); | ||
}, | ||
group: "peekPromiseState (rejected)", | ||
baseline: true, | ||
}); | ||
|
||
Deno.bench({ | ||
name: "current", | ||
fn: async () => { | ||
await peekPromiseState(new Promise(() => {})); | ||
}, | ||
group: "peekPromiseState (pending)", | ||
baseline: true, | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { test } from "@cross/test"; | ||
import { assertEquals } from "@std/assert"; | ||
import { flushPromises } from "./flush_promises.ts"; | ||
import { peekPromiseState } from "./peek_promise_state.ts"; | ||
|
||
test( | ||
"peekPromiseState() returns 'fulfilled' for resolved promise", | ||
async () => { | ||
const p = Promise.resolve("Resolved promise"); | ||
assertEquals(await peekPromiseState(p), "fulfilled"); | ||
}, | ||
); | ||
|
||
test( | ||
"peekPromiseState() returns 'rejected' for rejected promise", | ||
async () => { | ||
const p = Promise.reject("Rejected promise"); | ||
p.catch(() => undefined); // Avoid 'Uncaught (in promise) Rejected promise' | ||
assertEquals(await peekPromiseState(p), "rejected"); | ||
}, | ||
); | ||
|
||
test( | ||
"peekPromiseState() returns 'pending' for not resolved promise", | ||
async () => { | ||
const p = new Promise(() => undefined); | ||
assertEquals(await peekPromiseState(p), "pending"); | ||
}, | ||
); | ||
|
||
test("peekPromiseState() return the current state of the promise", async () => { | ||
const p = Promise.resolve<void>(undefined) | ||
.then(() => {}) | ||
.then(() => {}); | ||
assertEquals(await peekPromiseState(p), "pending"); | ||
await flushPromises(); | ||
assertEquals(await peekPromiseState(p), "fulfilled"); | ||
}); |
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
Oops, something went wrong.