feat(utils): Introduce promiseAll util (#5543)

This commit is contained in:
Adrien de Peretti
2023-11-08 08:48:48 +01:00
committed by GitHub
parent e4ce2f4e07
commit f90ba02087
99 changed files with 464 additions and 297 deletions

View File

@@ -0,0 +1,58 @@
import { promiseAll } from "../promise-all"
import { EOL } from "os"
describe("promiseAll", function () {
it("should throw an error if any of the promises throw", async function () {
const res = await promiseAll([
Promise.resolve(1),
(async () => {
throw new Error("error")
})(),
Promise.resolve(3),
]).catch((e) => e)
expect(res.message).toBe("error")
})
it("should throw errors if any of the promises throw and aggregate them", async function () {
const res = await promiseAll(
[
Promise.resolve(1),
(async () => {
throw new Error("error")
})(),
(async () => {
throw new Error("error2")
})(),
Promise.resolve(3),
],
{
aggregateErrors: true,
}
).catch((e) => e)
expect(res.message).toBe(["error", "error2"].join(EOL))
})
it("should return all values if all promises are fulfilled", async function () {
const res = await promiseAll([
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
])
expect(res).toEqual([1, 2, 3])
})
it("should return all values if all promises are fulfilled including waiting for nested promises", async function () {
const res = await promiseAll([
Promise.resolve(1),
(async () => {
await promiseAll([Promise.resolve(1), Promise.resolve(2)])
})(),
Promise.resolve(3),
])
expect(res).toEqual([1, undefined, 3])
})
})