Skip to content

Instantly share code, notes, and snippets.

@mhofman
Last active December 16, 2021 02:41
Show Gist options
  • Save mhofman/df7047ee89942038cbc4629c41b8c7e0 to your computer and use it in GitHub Desktop.
Save mhofman/df7047ee89942038cbc4629c41b8c7e0 to your computer and use it in GitHub Desktop.
What realm does a promise `onResolve` belong to?
var red = $262.createRealm().global;
var blue = $262.createRealm().global;
var yellow = $262.createRealm().global;
var purple = $262.createRealm().global;
var thenResult = {};
var finallyResult = {};
var who = new Map();
who.set(red.Function, "red");
who.set(blue.Function, "blue");
who.set(yellow.Function, "yellow");
who.set(purple.Function, "purple");
who.set(Function, "self");
var blueThenFactory = blue.eval(
`(result) => (onThen, onCatch) => { result.onThen = onThen; result.onCatch = onCatch; }`
);
var purpleOnResolveBlueThenable = purple.eval(
`(then) => () => ({ then: then })`
)(blueThenFactory(thenResult));
var redPromise = red.Promise.resolve();
yellow.Promise.prototype.then.call(redPromise, purpleOnResolveBlueThenable);
var blueThenable = { then: blueThenFactory(finallyResult) };
var purpleOnFinally = purple.eval(`() => {}`);
yellow.Promise.prototype.finally.call(blueThenable, purpleOnFinally);
print(who.size);
Promise.resolve()
.then(() => {})
.then(() => {
print(
"then",
who.get(thenResult.onThen.constructor),
who.get(thenResult.onCatch.constructor)
);
print(
"finally",
who.get(finallyResult.onThen.constructor),
who.get(finallyResult.onCatch.constructor)
);
});
#### engine262
5
then red red
finally yellow yellow
#### JavaScriptCore
5
then red red
finally yellow yellow
#### Moddable XS
1
then self self
finally self self
#### SpiderMonkey
5
then blue blue
finally yellow yellow
#### V8
5
then blue blue
finally yellow yellow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment