GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: end rejection cycles in handled-value marking (#744) · tinylibs/tinybench@bcaad8a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • async.ts
  • test
    • run-sync-hook-rejection.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/async.ts‎

Copy file name to clipboardExpand all lines: src/async.ts
+30-11Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import type { Fn } from './types'
22

3-
import { emptyFunction } from './constants'
4-
53
/**
64
* Checks whether a value is a promise-like object.
75
* @param maybePromiseLike - the value to check
@@ -16,18 +14,39 @@ export const isPromiseLike = <T>(
1614
typeof (maybePromiseLike as PromiseLike<T>).then === 'function'
1715

1816
/**
19-
* Suppresses rejections of a promise-like value, and of the promise its own
20-
* `then` call returns, so neither surfaces as an unhandled rejection.
17+
* Suppresses rejections of a promise-like value across its rejection channel,
18+
* the promise its own `then` call returns, and promise-like values it settles
19+
* with, so none surfaces as an unhandled rejection. Only raw handlers are
20+
* attached, never assimilated, so handling is realm-agnostic and bounded:
21+
* every value is attached at most once per probe, so a value reachable
22+
* through several channels costs a single attach, and settled values and
23+
* returned promises are followed with a budget of `depth` levels only, which
24+
* ends rejection and fulfillment cycles of any shape and keeps a self-nesting
25+
* thenable from generating work endlessly. A thenable that keeps settling new
26+
* values on persistent handlers can still keep making work on its own; no
27+
* probe can detach from it.
2128
* @param value - the promise-like value to mark handled
29+
* @param depth - the remaining budget for following settled values
30+
* @param seen - values already attached to by this probe
2231
*/
23-
export const markHandled = (value: unknown): void => {
32+
export const markHandled = (
33+
value: unknown,
34+
depth = 2,
35+
seen: WeakSet<object> = new WeakSet()
36+
): void => {
2437
try {
25-
const returned = (value as PromiseLike<unknown>).then(
26-
emptyFunction,
27-
emptyFunction
28-
)
29-
if (isPromiseLike(returned)) {
30-
Promise.resolve(returned).catch(emptyFunction)
38+
if (!isPromiseLike(value) || seen.has(value)) {
39+
return
40+
}
41+
seen.add(value)
42+
const settle = (settled: unknown): void => {
43+
if (depth >= 0) {
44+
markHandled(settled, depth - 1, seen)
45+
}
46+
}
47+
const returned = value.then(settle, settle)
48+
if (depth >= 0) {
49+
markHandled(returned, depth - 1, seen)
3150
}
3251
} catch {
3352
// Treat a throwing `then` as handled

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.