Skip to content

Commit 1135035

Browse files
committed
Replace the test helper class Deferred with polyfilled Promise.withResolvers.
1 parent 1bc03b9 commit 1135035

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- Integrated a new dev dependency [`eslint-plugin-jsdoc`](https://npm.im/eslint-plugin-jsdoc) and revised types.
4242
- Removed the Node.js CLI option `--unhandled-rejections=throw` in the package script `tests` as it’s now the default for all supported Node.js versions.
4343
- Avoid hardcoding a default value in the type `FileUploadCreateReadStreamOptions` property `highWaterMark` description and use the function `getDefaultHighWaterMark` from `node:stream` in tests.
44+
- Replaced the test helper class `Deferred` with polyfilled `Promise.withResolvers`.
4445
- Omit unused catch bindings in the function `processRequest`.
4546
- Corrected the JSDoc type `FileUploadCreateReadStreamOptions` in the module `processRequest.mjs`.
4647
- Avoid using `return` in the middleware.

processRequest.test.mjs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22

33
import "./test/polyfillFile.mjs";
4+
import "./test/polyfillPromiseWithResolvers.mjs";
45

56
import {
67
deepStrictEqual,
@@ -20,7 +21,6 @@ import { ReadStream } from "fs-capacitor";
2021

2122
import processRequest from "./processRequest.mjs";
2223
import abortingMultipartRequest from "./test/abortingMultipartRequest.mjs";
23-
import Deferred from "./test/Deferred.mjs";
2424
import Upload from "./Upload.mjs";
2525

2626
describe(
@@ -794,11 +794,15 @@ describe(
794794
// request part way through, the server request handler must be manually
795795
// awaited or else the test will resolve and the process will exit before
796796
// it’s done.
797-
const done = new Deferred();
797+
798+
/** @type {PromiseWithResolvers<void>} */
799+
const done = Promise.withResolvers();
798800

799801
// The request must be aborted after it has been received by the server
800802
// request handler, or else Node.js won’t run the handler.
801-
const requestReceived = new Deferred();
803+
804+
/** @type {PromiseWithResolvers<void>} */
805+
const requestReceived = Promise.withResolvers();
802806

803807
const server = createServer(async (request, response) => {
804808
try {
@@ -936,11 +940,15 @@ describe(
936940
// request part way through, the server request handler must be manually
937941
// awaited or else the test will resolve and the process will exit before
938942
// it’s done.
939-
const done = new Deferred();
943+
944+
/** @type {PromiseWithResolvers<void>} */
945+
const done = Promise.withResolvers();
940946

941947
// The request must be aborted after it has been received by the server
942948
// request handler, or else Node.js won’t run the handler.
943-
const requestReceived = new Deferred();
949+
950+
/** @type {PromiseWithResolvers<void>} */
951+
const requestReceived = Promise.withResolvers();
944952

945953
const server = createServer(async (request, response) => {
946954
try {

test/Deferred.mjs

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/polyfillPromiseWithResolvers.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @ts-check
2+
3+
// TODO: Delete this polyfill once all supported Node.js versions implement
4+
// `Promise.withResolvers`.
5+
Promise.withResolvers ??= () => new PromiseWithResolversReturn();
6+
7+
/**
8+
* A promise with resolvers.
9+
* @template T
10+
* @implements {PromiseWithResolvers<T>}
11+
*/
12+
class PromiseWithResolversReturn {
13+
constructor() {
14+
/** @type {Promise<T>} */
15+
this.promise = new Promise((resolve, reject) => {
16+
this.resolve = resolve;
17+
this.reject = reject;
18+
});
19+
}
20+
}

0 commit comments

Comments
 (0)