@lowerdeck/programmable-promise 1.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.
@@ -0,0 +1,11 @@
1
+
2
+ $ microbundle
3
+ Build "@lowerdeck/programmable-promise" to dist:
4
+ 482 B: index.cjs.gz
5
+ 417 B: index.cjs.br
6
+ 173 B: index.modern.js.gz
7
+ 148 B: index.modern.js.br
8
+ 487 B: index.module.js.gz
9
+ 418 B: index.module.js.br
10
+ 555 B: index.umd.js.gz
11
+ 480 B: index.umd.js.br
@@ -0,0 +1,24 @@
1
+
2
+ $ vitest run --passWithNoTests
3
+ [?25l
4
+  RUN  v3.2.4 /Users/tobias/code/metorial/metorial-enterprise/oss/src/packages/shared/programmable-promise
5
+
6
+ [?2026h
7
+  ❯ src/index.test.ts [queued]
8
+
9
+  Test Files 0 passed (1)
10
+  Tests 0 passed (0)
11
+  Start at 10:23:27
12
+  Duration 101ms
13
+ [?2026l ✓ src/index.test.ts (4 tests) 3ms
14
+ ✓ ProgrammablePromise > should resolve with the provided value 1ms
15
+ ✓ ProgrammablePromise > should reject with the provided reason 1ms
16
+ ✓ ProgrammablePromise > should allow accessing the resolved value after resolution 0ms
17
+ ✓ ProgrammablePromise > should not have a value before resolution 0ms
18
+
19
+  Test Files  1 passed (1)
20
+  Tests  4 passed (4)
21
+  Start at  10:23:27
22
+  Duration  217ms (transform 29ms, setup 0ms, collect 28ms, tests 3ms, environment 0ms, prepare 41ms)
23
+
24
+ [?25h
package/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # `@lowerdeck/programmable-promise`
2
+
3
+ Promise with externally controllable resolve and reject. Allows you to create a promise and settle it from outside, useful for complex control flow patterns.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @lowerdeck/programmable-promise
9
+ yarn add @lowerdeck/programmable-promise
10
+ bun add @lowerdeck/programmable-promise
11
+ pnpm add @lowerdeck/programmable-promise
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```typescript
17
+ import { ProgrammablePromise } from '@lowerdeck/programmable-promise';
18
+
19
+ // Create a promise you can control externally
20
+ const pp = new ProgrammablePromise<string>();
21
+
22
+ // Pass the promise around
23
+ setTimeout(() => {
24
+ pp.resolve('Hello!');
25
+ }, 1000);
26
+
27
+ const result = await pp.promise;
28
+ console.log(result); // 'Hello!'
29
+
30
+ // Useful for event-driven flows
31
+ class EventHandler {
32
+ private completion = new ProgrammablePromise<void>();
33
+
34
+ onComplete() {
35
+ return this.completion.promise;
36
+ }
37
+
38
+ handleEvent(event: Event) {
39
+ if (event.type === 'done') {
40
+ this.completion.resolve();
41
+ }
42
+ }
43
+ }
44
+
45
+ // Access resolved value without awaiting
46
+ const pp2 = new ProgrammablePromise<number>();
47
+ pp2.resolve(42);
48
+ console.log(pp2.value); // 42
49
+ ```
50
+
51
+ ## License
52
+
53
+ This project is licensed under the Apache License 2.0.
54
+
55
+ <div align="center">
56
+ <sub>Built with ❤️ by <a href="https://metorial.com">Metorial</a></sub>
57
+ </div>
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@lowerdeck/programmable-promise",
3
+ "version": "1.0.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "author": "Tobias Herber",
8
+ "license": "Apache 2",
9
+ "type": "module",
10
+ "source": "src/index.ts",
11
+ "exports": {
12
+ "require": "./dist/index.cjs",
13
+ "default": "./dist/index.modern.js"
14
+ },
15
+ "main": "./dist/index.cjs",
16
+ "module": "./dist/index.module.js",
17
+ "types": "dist/index.d.ts",
18
+ "unpkg": "./dist/index.umd.js",
19
+ "scripts": {
20
+ "test": "vitest run --passWithNoTests",
21
+ "lint": "prettier src/**/*.ts --check",
22
+ "build": "microbundle"
23
+ },
24
+ "dependencies": {},
25
+ "devDependencies": {
26
+ "microbundle": "^0.15.1",
27
+ "@lowerdeck/tsconfig": "^1.0.0",
28
+ "typescript": "^5.8.3",
29
+ "vitest": "^3.1.2"
30
+ }
31
+ }
@@ -0,0 +1,32 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { ProgrammablePromise } from './index';
3
+
4
+ describe('ProgrammablePromise', () => {
5
+ it('should resolve with the provided value', async () => {
6
+ const promise = new ProgrammablePromise<number>();
7
+ promise.resolve(42);
8
+ const result = await promise.promise;
9
+ expect(result).toBe(42);
10
+ expect(promise.value).toBe(42);
11
+ });
12
+
13
+ it('should reject with the provided reason', async () => {
14
+ const promise = new ProgrammablePromise<number>();
15
+ const error = new Error('Test error');
16
+ promise.reject(error);
17
+
18
+ await expect(promise.promise).rejects.toThrow('Test error');
19
+ });
20
+
21
+ it('should allow accessing the resolved value after resolution', async () => {
22
+ const promise = new ProgrammablePromise<string>();
23
+ promise.resolve('Hello, world!');
24
+ await promise.promise;
25
+ expect(promise.value).toBe('Hello, world!');
26
+ });
27
+
28
+ it('should not have a value before resolution', () => {
29
+ const promise = new ProgrammablePromise<number>();
30
+ expect(promise.value).toBeUndefined();
31
+ });
32
+ });
package/src/index.ts ADDED
@@ -0,0 +1,30 @@
1
+ export class ProgrammablePromise<T> {
2
+ private _resolve!: (value: T | PromiseLike<T>) => void;
3
+ private _reject!: (reason?: any) => void;
4
+ private _promise: Promise<T>;
5
+ private _value!: T;
6
+
7
+ constructor() {
8
+ this._promise = new Promise<T>((resolve, reject) => {
9
+ this._resolve = resolve;
10
+ this._reject = reject;
11
+ });
12
+ }
13
+
14
+ get promise() {
15
+ return this._promise;
16
+ }
17
+
18
+ resolve(value: T | PromiseLike<T>) {
19
+ this._resolve(value);
20
+ this._value = value as T;
21
+ }
22
+
23
+ get reject() {
24
+ return this._reject;
25
+ }
26
+
27
+ get value() {
28
+ return this._value;
29
+ }
30
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "@lowerdeck/tsconfig/base.json",
4
+ "exclude": [
5
+ "dist"
6
+ ],
7
+ "include": [
8
+ "src"
9
+ ],
10
+ "compilerOptions": {
11
+ "outDir": "dist"
12
+ }
13
+ }