Skip to content

Commit a3b54b2

Browse files
authored
fix stale rejection (#365)
* fix stale rejection * test for stale fulfillments & rejections
1 parent 5116a31 commit a3b54b2

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ function variable_compute(variable) {
276276
variable._value = value;
277277
variable._fulfilled(value);
278278
}, (error) => {
279-
if (error === variable_stale) return;
279+
if (error === variable_stale || variable._version !== version) return;
280280
variable._value = undefined;
281281
variable._rejected(error);
282282
});

test/variable/define-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,49 @@ it("variable.define allows other variables to begin computation before a generat
486486
assert.strictEqual(await gen._promise, 3, "gen cell 3");
487487
assert.strictEqual(await val._promise, 3, "val cell 3");
488488
});
489+
490+
it("variable.define does not report stale fulfillments", async () => {
491+
const runtime = new Runtime();
492+
const module = runtime.module();
493+
const values = [];
494+
const errors = [];
495+
const variable = module.variable({
496+
fulfilled(value) {
497+
values.push(value);
498+
},
499+
rejected(error) {
500+
errors.push(error);
501+
}
502+
});
503+
const promise = new Promise((resolve) => setTimeout(() => resolve("value1"), 250));
504+
variable.define(() => promise);
505+
await runtime._computing;
506+
variable.define(() => "value2");
507+
await promise;
508+
assert.deepStrictEqual(await valueof(variable), {value: "value2"});
509+
assert.deepStrictEqual(values, ["value2"]);
510+
assert.deepStrictEqual(errors, []);
511+
});
512+
513+
it("variable.define does not report stale rejections", async () => {
514+
const runtime = new Runtime();
515+
const module = runtime.module();
516+
const values = [];
517+
const errors = [];
518+
const variable = module.variable({
519+
fulfilled(value) {
520+
values.push(value);
521+
},
522+
rejected(error) {
523+
errors.push(error);
524+
}
525+
});
526+
const promise = new Promise((resolve, reject) => setTimeout(() => reject("error1"), 250));
527+
variable.define(() => promise);
528+
await runtime._computing;
529+
variable.define(() => Promise.reject("error2"));
530+
await promise.catch(() => {});
531+
assert.deepStrictEqual(await valueof(variable), {error: "error2"});
532+
assert.deepStrictEqual(values, []);
533+
assert.deepStrictEqual(errors, ["error2"]);
534+
});

0 commit comments

Comments
 (0)