Skip to content

Commit 641df4b

Browse files
Align reference implementation of "wait for all" with latest Web IDL spec
The spec of "get a promise for waiting for all" changed slightly when it was moved from the promise guide to Web IDL. Notably, the successSteps and failureSteps arguments were removed, since you can already achieve the same thing by reacting to the returned promise. This updates the reference implementation to align with the definitions from the Web IDL specification. No normative changes.
1 parent 5aec42e commit 641df4b

File tree

2 files changed

+17
-37
lines changed

2 files changed

+17
-37
lines changed

reference-implementation/lib/abstract-ops/readable-streams.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventC
164164
return promiseResolvedWith(undefined);
165165
});
166166
}
167-
shutdownWithAction(() => waitForAllPromise(actions.map(action => action()), results => results), true, error);
167+
shutdownWithAction(() => waitForAllPromise(actions.map(action => action())), true, error);
168168
};
169169

170170
if (signal.aborted === true) {

reference-implementation/lib/helpers/webidl.js

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const originalPromiseReject = Promise.reject;
88

99
const promiseSideTable = new WeakMap();
1010

11-
// https://heycam.github.io/webidl/#a-new-promise
11+
// https://webidl.spec.whatwg.org/#a-new-promise
1212
function newPromise() {
1313
// The stateIsPending tracking only works if we never resolve the promises with other promises.
1414
// In this spec, that happens to be true for the promises in question; they are always resolved with undefined.
@@ -22,7 +22,7 @@ function newPromise() {
2222
return promise;
2323
}
2424

25-
// https://heycam.github.io/webidl/#resolve
25+
// https://webidl.spec.whatwg.org/#resolve
2626
function resolvePromise(p, value) {
2727
// We intend to only resolve or reject promises that are still pending.
2828
// When this is not the case, it usually means there's a bug in the specification that we want to fix.
@@ -33,22 +33,22 @@ function resolvePromise(p, value) {
3333
promiseSideTable.get(p).stateIsPending = false;
3434
}
3535

36-
// https://heycam.github.io/webidl/#reject
36+
// https://webidl.spec.whatwg.org/#reject
3737
function rejectPromise(p, reason) {
3838
assert(stateIsPending(p) === true);
3939
promiseSideTable.get(p).reject(reason);
4040
promiseSideTable.get(p).stateIsPending = false;
4141
}
4242

43-
// https://heycam.github.io/webidl/#a-promise-resolved-with
43+
// https://webidl.spec.whatwg.org/#a-promise-resolved-with
4444
function promiseResolvedWith(value) {
4545
// Cannot use original Promise.resolve since that will return value itself sometimes, unlike Web IDL.
4646
const promise = new originalPromise(resolve => resolve(value));
4747
promiseSideTable.set(promise, { stateIsPending: false });
4848
return promise;
4949
}
5050

51-
// https://heycam.github.io/webidl/#a-promise-rejected-with
51+
// https://webidl.spec.whatwg.org/#a-promise-rejected-with
5252
function promiseRejectedWith(reason) {
5353
const promise = originalPromiseReject.call(originalPromise, reason);
5454
promiseSideTable.set(promise, { stateIsPending: false });
@@ -61,7 +61,7 @@ function PerformPromiseThen(promise, onFulfilled, onRejected) {
6161
return originalPromiseThen.call(promise, onFulfilled, onRejected);
6262
}
6363

64-
// https://heycam.github.io/webidl/#dfn-perform-steps-once-promise-is-settled
64+
// https://webidl.spec.whatwg.org/#dfn-perform-steps-once-promise-is-settled
6565
function uponPromise(promise, onFulfilled, onRejected) {
6666
PerformPromiseThen(
6767
PerformPromiseThen(promise, onFulfilled, onRejected),
@@ -104,8 +104,9 @@ Object.assign(exports, {
104104
stateIsPending
105105
});
106106

107-
// https://heycam.github.io/webidl/#wait-for-all
107+
// https://webidl.spec.whatwg.org/#wait-for-all
108108
function waitForAll(promises, successSteps, failureSteps) {
109+
let fulfilledCount = 0;
109110
let rejected = false;
110111
const rejectionHandler = arg => {
111112
if (rejected === false) {
@@ -114,7 +115,6 @@ function waitForAll(promises, successSteps, failureSteps) {
114115
}
115116
};
116117
let index = 0;
117-
let fulfilledCount = 0;
118118
const total = promises.length;
119119
const result = new Array(total);
120120
if (total === 0) {
@@ -135,35 +135,15 @@ function waitForAll(promises, successSteps, failureSteps) {
135135
}
136136
}
137137

138-
// https://heycam.github.io/webidl/#waiting-for-all-promise
139-
exports.waitForAllPromise = (promises, successSteps, failureSteps = undefined) => {
140-
let resolveP;
141-
let rejectP;
142-
const promise = new Promise((resolve, reject) => {
143-
resolveP = resolve;
144-
rejectP = reject;
145-
});
146-
if (failureSteps === undefined) {
147-
failureSteps = arg => {
148-
throw arg;
149-
};
150-
}
151-
const successStepsWrapper = results => {
152-
try {
153-
const stepsResult = successSteps(results);
154-
resolveP(stepsResult);
155-
} catch (e) {
156-
rejectP(e);
157-
}
138+
// https://webidl.spec.whatwg.org/#waiting-for-all-promise
139+
exports.waitForAllPromise = promises => {
140+
const promise = newPromise();
141+
const successSteps = results => {
142+
resolvePromise(promise, results);
158143
};
159-
const failureStepsWrapper = reason => {
160-
try {
161-
const stepsResult = failureSteps(reason);
162-
resolveP(stepsResult);
163-
} catch (e) {
164-
rejectP(e);
165-
}
144+
const failureSteps = reason => {
145+
rejectPromise(promise, reason);
166146
};
167-
waitForAll(promises, successStepsWrapper, failureStepsWrapper);
147+
waitForAll(promises, successSteps, failureSteps);
168148
return promise;
169149
};

0 commit comments

Comments
 (0)