-
-
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.
feat(flushPromises): add
flushPromises
function
- Loading branch information
1 parent
686fd39
commit cd7a48b
Showing
4 changed files
with
64 additions
and
0 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,23 @@ | ||
/** | ||
* Flushes all pending promises. | ||
* | ||
* ```ts | ||
* import { flushPromises } from "@core/asyncutil/flush-promises"; | ||
* | ||
* let count = 0; | ||
* Array.from({ length: 10 }).forEach(() => { | ||
* new Promise<void>((resolve) => resolve()).then(() => { | ||
* count++; | ||
* }); | ||
* }); | ||
* | ||
* console.log(count); // 0 | ||
* | ||
* await flushPromises(); | ||
* | ||
* console.log(count); // 10 | ||
* ``` | ||
*/ | ||
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() wait all promises are resolves", | ||
async () => { | ||
let count = 0; | ||
Array.from({ length: 10 }).forEach(() => { | ||
new Promise<void>((resolve) => resolve()).then(() => { | ||
count++; | ||
}); | ||
}); | ||
assertEquals(count, 0); | ||
await flushPromises(); | ||
assertEquals(count, 10); | ||
}, | ||
); |