hn.today

Fixing a WebKit bug to see if promises are made of train tracks

alistair.sh5 points0 comments
Screenshot of Fixing a WebKit bug to see if promises are made of train tracks

A small JavaScript program exposes a WebKit bug: after changing a promise’s `constructor` so its `Symbol.species` getter throws, `await` reports the error but never finishes. Node.js rejects the promise and exits; Safari and Bun, which both use JavaScriptCore, leave it pending indefinitely. The explanation starts with a key distinction in promise behavior: rejecting immediately records the supplied reason, but resolving first checks whether the value has a callable `then` property. If so, the promise adopts that thenable’s eventual outcome rather than storing it as its value. Reading `then` can itself throw, in which case resolution must reject. A promise can therefore be locked into an outcome while still pending, for example when it adopts a promise that never settles.

The failure occurs because `await` normally has a shortcut for an unmodified native promise. Changing the promise’s `constructor` disables that shortcut, so `await` treats it as a thenable and calls its `then` method. That method must inspect the constructor and `Symbol.species` to choose the type of promise it returns; the throwing getter triggers an abrupt completion. The specification requires the resulting promise to be rejected with that error, but JavaScriptCore fails to complete it. The discussion connects promise chaining to railway-style error handling, where success and failure follow separate paths and later steps are skipped after failure. It concludes with a WebKit fix of just 16 added lines for bug 318399.

Read on alistair.sh0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Web

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.