MCPcopy
hub / github.com/vercel/next.js / AwaiterMulti

Class AwaiterMulti

packages/next/src/server/after/awaiter.ts:9–39  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7 * that second promise will also be awaited.
8 */
9export class AwaiterMulti {
10 private promises: Set<Promise<unknown>> = new Set()
11 private onError: (error: unknown) => void
12
13 constructor({ onError }: { onError?: (error: unknown) => void } = {}) {
14 this.onError = onError ?? console.error
15 }
16
17 public waitUntil = (promise: Promise<unknown>): void => {
18 // if a promise settles before we await it, we should drop it --
19 // storing them indefinitely could result in a memory leak.
20 const cleanup = () => {
21 this.promises.delete(promise)
22 }
23
24 promise.then(cleanup, (err) => {
25 cleanup()
26 this.onError(err)
27 })
28
29 this.promises.add(promise)
30 }
31
32 public async awaiting(): Promise<void> {
33 while (this.promises.size > 0) {
34 const promises = Array.from(this.promises)
35 this.promises.clear()
36 await Promise.allSettled(promises)
37 }
38 }
39}
40
41/**
42 * Like {@link AwaiterMulti}, but can only be awaited once.

Callers

nothing calls this directly

Calls 4

thenMethod · 0.80
cleanupFunction · 0.50
onErrorMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected