@prairielearn/utils 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +0 -0
- package/CHANGELOG.md +7 -0
- package/README.md +23 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +15 -0
- package/dist/index.test.js.map +1 -0
- package/package.json +24 -0
- package/src/index.test.ts +17 -0
- package/src/index.ts +27 -0
- package/tsconfig.json +7 -0
- package/vitest.config.ts +11 -0
|
File without changes
|
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# `@prairielearn/utils`
|
|
2
|
+
|
|
3
|
+
Various shared utilities.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
### `withResolvers()`
|
|
8
|
+
|
|
9
|
+
A tiny utility for creating Promises with exposed `resolve` and `reject` methods, similar to [`Promise.withResolvers()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers).
|
|
10
|
+
|
|
11
|
+
Once `Promise.withResolvers()` is widely supported in browsers, users of this package should switch to it.
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { withResolvers } from '@prairielearn/utils';
|
|
15
|
+
|
|
16
|
+
const { promise, resolve, reject } = withResolvers<number>();
|
|
17
|
+
|
|
18
|
+
setTimeout(() => resolve(42), 100);
|
|
19
|
+
|
|
20
|
+
promise.then((value) => {
|
|
21
|
+
console.log(value); // 42
|
|
22
|
+
});
|
|
23
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An object containing a promise and its resolve/reject methods.
|
|
3
|
+
*/
|
|
4
|
+
export interface PromiseWithResolvers<T> {
|
|
5
|
+
/** The promise instance. */
|
|
6
|
+
promise: Promise<T>;
|
|
7
|
+
/** Resolves the promise. */
|
|
8
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
9
|
+
/** Rejects the promise. */
|
|
10
|
+
reject: (reason?: any) => void;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Returns an object with a promise and its resolve/reject methods exposed.
|
|
14
|
+
* This is similar to Node.js's util.withResolvers (Node 21+).
|
|
15
|
+
*
|
|
16
|
+
* @returns An object containing the promise, resolve, and reject.
|
|
17
|
+
*/
|
|
18
|
+
export declare function withResolvers<T>(): PromiseWithResolvers<T>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns an object with a promise and its resolve/reject methods exposed.
|
|
3
|
+
* This is similar to Node.js's util.withResolvers (Node 21+).
|
|
4
|
+
*
|
|
5
|
+
* @returns An object containing the promise, resolve, and reject.
|
|
6
|
+
*/
|
|
7
|
+
export function withResolvers() {
|
|
8
|
+
let resolve;
|
|
9
|
+
let reject;
|
|
10
|
+
const promise = new Promise((res, rej) => {
|
|
11
|
+
resolve = res;
|
|
12
|
+
reject = rej;
|
|
13
|
+
});
|
|
14
|
+
return { promise, resolve: resolve, reject: reject };
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAYA;;;;;GAKG;AACH,MAAM,UAAU,aAAa;IAC3B,IAAI,OAA4C,CAAC;IACjD,IAAI,MAA8B,CAAC;IACnC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAI,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC1C,OAAO,GAAG,GAAG,CAAC;QACd,MAAM,GAAG,GAAG,CAAC;IACf,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAQ,EAAE,MAAM,EAAE,MAAO,EAAE,CAAC;AACzD,CAAC","sourcesContent":["/**\n * An object containing a promise and its resolve/reject methods.\n */\nexport interface PromiseWithResolvers<T> {\n /** The promise instance. */\n promise: Promise<T>;\n /** Resolves the promise. */\n resolve: (value: T | PromiseLike<T>) => void;\n /** Rejects the promise. */\n reject: (reason?: any) => void;\n}\n\n/**\n * Returns an object with a promise and its resolve/reject methods exposed.\n * This is similar to Node.js's util.withResolvers (Node 21+).\n *\n * @returns An object containing the promise, resolve, and reject.\n */\nexport function withResolvers<T>(): PromiseWithResolvers<T> {\n let resolve: (value: T | PromiseLike<T>) => void;\n let reject: (reason?: any) => void;\n const promise = new Promise<T>((res, rej) => {\n resolve = res;\n reject = rej;\n });\n return { promise, resolve: resolve!, reject: reject! };\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { withResolvers } from './index.js';
|
|
3
|
+
describe('withResolvers', () => {
|
|
4
|
+
it('resolves with the correct value', async () => {
|
|
5
|
+
const { promise, resolve } = withResolvers();
|
|
6
|
+
setTimeout(() => resolve(123), 10);
|
|
7
|
+
await expect(promise).resolves.toBe(123);
|
|
8
|
+
});
|
|
9
|
+
it('rejects with the correct reason', async () => {
|
|
10
|
+
const { promise, reject } = withResolvers();
|
|
11
|
+
setTimeout(() => reject(new Error('fail')), 10);
|
|
12
|
+
await expect(promise).rejects.toThrow('fail');
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
//# sourceMappingURL=index.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAE9C,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3C,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC7B,EAAE,CAAC,iCAAiC,EAAE,KAAK,IAAI,EAAE;QAC/C,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,aAAa,EAAU,CAAC;QACrD,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;QACnC,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3C,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,iCAAiC,EAAE,KAAK,IAAI,EAAE;QAC/C,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,aAAa,EAAU,CAAC;QACpD,UAAU,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAChD,MAAM,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAChD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { describe, expect, it } from 'vitest';\n\nimport { withResolvers } from './index.js';\n\ndescribe('withResolvers', () => {\n it('resolves with the correct value', async () => {\n const { promise, resolve } = withResolvers<number>();\n setTimeout(() => resolve(123), 10);\n await expect(promise).resolves.toBe(123);\n });\n\n it('rejects with the correct reason', async () => {\n const { promise, reject } = withResolvers<number>();\n setTimeout(() => reject(new Error('fail')), 10);\n await expect(promise).rejects.toThrow('fail');\n });\n});\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prairielearn/utils",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/PrairieLearn/PrairieLearn.git",
|
|
8
|
+
"directory": "packages/utils"
|
|
9
|
+
},
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"dev": "tsc --watch --preserveWatchOutput",
|
|
14
|
+
"test": "vitest run --coverage"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@prairielearn/tsconfig": "^0.0.0",
|
|
18
|
+
"@types/node": "^22.18.0",
|
|
19
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
20
|
+
"tsx": "^4.20.5",
|
|
21
|
+
"typescript": "^5.9.2",
|
|
22
|
+
"vitest": "^3.2.4"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { withResolvers } from './index.js';
|
|
4
|
+
|
|
5
|
+
describe('withResolvers', () => {
|
|
6
|
+
it('resolves with the correct value', async () => {
|
|
7
|
+
const { promise, resolve } = withResolvers<number>();
|
|
8
|
+
setTimeout(() => resolve(123), 10);
|
|
9
|
+
await expect(promise).resolves.toBe(123);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('rejects with the correct reason', async () => {
|
|
13
|
+
const { promise, reject } = withResolvers<number>();
|
|
14
|
+
setTimeout(() => reject(new Error('fail')), 10);
|
|
15
|
+
await expect(promise).rejects.toThrow('fail');
|
|
16
|
+
});
|
|
17
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An object containing a promise and its resolve/reject methods.
|
|
3
|
+
*/
|
|
4
|
+
export interface PromiseWithResolvers<T> {
|
|
5
|
+
/** The promise instance. */
|
|
6
|
+
promise: Promise<T>;
|
|
7
|
+
/** Resolves the promise. */
|
|
8
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
9
|
+
/** Rejects the promise. */
|
|
10
|
+
reject: (reason?: any) => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Returns an object with a promise and its resolve/reject methods exposed.
|
|
15
|
+
* This is similar to Node.js's util.withResolvers (Node 21+).
|
|
16
|
+
*
|
|
17
|
+
* @returns An object containing the promise, resolve, and reject.
|
|
18
|
+
*/
|
|
19
|
+
export function withResolvers<T>(): PromiseWithResolvers<T> {
|
|
20
|
+
let resolve: (value: T | PromiseLike<T>) => void;
|
|
21
|
+
let reject: (reason?: any) => void;
|
|
22
|
+
const promise = new Promise<T>((res, rej) => {
|
|
23
|
+
resolve = res;
|
|
24
|
+
reject = rej;
|
|
25
|
+
});
|
|
26
|
+
return { promise, resolve: resolve!, reject: reject! };
|
|
27
|
+
}
|
package/tsconfig.json
ADDED