watchable-promise 1.0.0 → 2.0.1

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # watchable-promise
2
2
 
3
- A Promise whose status you can read
3
+ A Promise whose state and value you can read
4
4
 
5
5
  ## Installation
6
6
 
@@ -13,12 +13,20 @@ npm install watchable-promise
13
13
  - Directly create a `WatchablePromise` instance:
14
14
 
15
15
  ```javascript
16
- import WatchablePromise from 'watchable-promise';
16
+ import WatchablePromise from "watchable-promise";
17
17
 
18
- const p = new WatchablePromise();
18
+ const p = new WatchablePromise(resolve => setTimeout(() => resolve("foo"), 100));
19
19
 
20
- console.log(p.status); // pending
20
+ console.log(p.state); // pending
21
21
  console.log(p.settled); // false
22
+ console.log(p.value); // undefined
23
+
24
+ const val = await p;
25
+
26
+ console.log(val) // "foo"
27
+ console.log(p.state); // fulfilled
28
+ console.log(p.settled); // true
29
+ console.log(p.value); // "foo"
22
30
  ```
23
31
 
24
32
  - Using an existing `Promise` instance:
@@ -26,17 +34,19 @@ npm install watchable-promise
26
34
  ```javascript
27
35
  import WatchablePromise from 'watchable-promise';
28
36
 
29
- const existingPromise = new Promise(resolve => setTimeout(() => resolve('foo'), 100));
37
+ const existingPromise = new Promise(resolve => setTimeout(() => resolve("foo"), 100));
30
38
  const p = WatchablePromise.from(existingPromise);
31
39
 
32
- console.log(p.status); // pending
40
+ console.log(p.state); // pending
33
41
  console.log(p.settled); // false
42
+ console.log(p.value); // undefined
43
+
44
+ const val = await p;
34
45
 
35
- p.then(val => {
36
- console.log(val) // 'bar'
37
- console.log(p.status); // fulfilled
38
- console.log(p.settled); // true
39
- });
46
+ console.log(val) // "foo"
47
+ console.log(p.state); // fulfilled
48
+ console.log(p.settled); // true
49
+ console.log(p.value); // "foo"
40
50
  ```
41
51
 
42
52
  - Using `WatchablePromise.withResolvers()`:
@@ -46,16 +56,15 @@ npm install watchable-promise
46
56
 
47
57
  const { promise, resolve, reject } = WatchablePromise.withResolvers();
48
58
 
49
- console.log(promise.status); // pending
59
+ console.log(promise.state); // pending
50
60
  console.log(promise.settled); // false
61
+ console.log(promise.value); // undefined
51
62
 
52
- resolve('bar');
63
+ await resolve("foo");
53
64
 
54
- promise.then(val => {
55
- console.log(val) // 'bar'
56
- console.log(promise.status); // fulfilled
57
- console.log(promise.settled); // true
58
- });
65
+ console.log(promise.state); // fulfilled
66
+ console.log(promise.settled); // true
67
+ console.log(promise.value); // "foo"
59
68
  ```
60
69
 
61
70
  ## License
package/dist/index.d.ts CHANGED
@@ -2,7 +2,8 @@ declare class WatchablePromise<T> extends Promise<T> {
2
2
  #private;
3
3
  constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void);
4
4
  get settled(): boolean;
5
- get status(): "pending" | "fulfilled" | "rejected";
5
+ get state(): "pending" | "fulfilled" | "rejected";
6
+ get value(): any;
6
7
  static from<T>(existingPromise: Promise<T>): WatchablePromise<T>;
7
8
  static withResolvers<T>(): {
8
9
  promise: WatchablePromise<T>;
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  // src/index.ts
2
2
  var WatchablePromise = class _WatchablePromise extends Promise {
3
3
  #settled = false;
4
- #status = "pending";
4
+ #state = "pending";
5
+ #value;
5
6
  constructor(executor) {
6
7
  let resolve;
7
8
  let reject;
@@ -10,12 +11,14 @@ var WatchablePromise = class _WatchablePromise extends Promise {
10
11
  reject = rej;
11
12
  });
12
13
  this.then(
13
- () => {
14
- this.#status = "fulfilled";
14
+ (value) => {
15
+ this.#state = "fulfilled";
16
+ this.#value = value;
15
17
  this.#settled = true;
16
18
  },
17
- () => {
18
- this.#status = "rejected";
19
+ (reason) => {
20
+ this.#state = "rejected";
21
+ this.#value = reason;
19
22
  this.#settled = true;
20
23
  }
21
24
  );
@@ -24,8 +27,11 @@ var WatchablePromise = class _WatchablePromise extends Promise {
24
27
  get settled() {
25
28
  return this.#settled;
26
29
  }
27
- get status() {
28
- return this.#status;
30
+ get state() {
31
+ return this.#state;
32
+ }
33
+ get value() {
34
+ return this.#value;
29
35
  }
30
36
  static from(existingPromise) {
31
37
  return new _WatchablePromise((resolve, reject) => {
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["class WatchablePromise<T> extends Promise<T> {\n #settled = false;\n #status: \"pending\" | \"fulfilled\" | \"rejected\" = \"pending\";\n\n constructor(\n executor: (\n resolve: (value: T | PromiseLike<T>) => void,\n reject: (reason?: any) => void,\n ) => void,\n ) {\n let resolve: (value: T | PromiseLike<T>) => void;\n let reject: (reason?: any) => void;\n\n super((res, rej) => {\n resolve = res;\n reject = rej;\n });\n\n this.then(\n () => {\n this.#status = \"fulfilled\";\n this.#settled = true;\n },\n () => {\n this.#status = \"rejected\";\n this.#settled = true;\n },\n );\n\n // @ts-ignore\n executor(resolve, reject);\n }\n\n get settled() {\n return this.#settled;\n }\n\n get status() {\n return this.#status;\n }\n\n static from<T>(existingPromise: Promise<T>): WatchablePromise<T> {\n return new WatchablePromise<T>((resolve, reject) => {\n existingPromise.then(resolve, reject);\n });\n }\n\n static withResolvers<T>() {\n let resolve: (value: T | PromiseLike<T>) => void;\n let reject: (reason?: any) => void;\n const promise = new WatchablePromise<T>((res, rej) => {\n resolve = res;\n reject = rej;\n });\n // @ts-ignore\n return { promise, resolve, reject };\n }\n\n static get [Symbol.species]() {\n return Promise;\n }\n\n get [Symbol.toStringTag]() {\n return \"WatchablePromise\";\n }\n}\n\nexport default WatchablePromise;\n"],
5
- "mappings": ";AAAA,IAAM,mBAAN,MAAM,0BAA4B,QAAW;AAAA,EAC3C,WAAW;AAAA,EACX,UAAgD;AAAA,EAEhD,YACE,UAIA;AACA,QAAI;AACJ,QAAI;AAEJ,UAAM,CAAC,KAAK,QAAQ;AAClB,gBAAU;AACV,eAAS;AAAA,IACX,CAAC;AAED,SAAK;AAAA,MACH,MAAM;AACJ,aAAK,UAAU;AACf,aAAK,WAAW;AAAA,MAClB;AAAA,MACA,MAAM;AACJ,aAAK,UAAU;AACf,aAAK,WAAW;AAAA,MAClB;AAAA,IACF;AAGA,aAAS,SAAS,MAAM;AAAA,EAC1B;AAAA,EAEA,IAAI,UAAU;AACZ,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,SAAS;AACX,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,OAAO,KAAQ,iBAAkD;AAC/D,WAAO,IAAI,kBAAoB,CAAC,SAAS,WAAW;AAClD,sBAAgB,KAAK,SAAS,MAAM;AAAA,IACtC,CAAC;AAAA,EACH;AAAA,EAEA,OAAO,gBAAmB;AACxB,QAAI;AACJ,QAAI;AACJ,UAAM,UAAU,IAAI,kBAAoB,CAAC,KAAK,QAAQ;AACpD,gBAAU;AACV,eAAS;AAAA,IACX,CAAC;AAED,WAAO,EAAE,SAAS,SAAS,OAAO;AAAA,EACpC;AAAA,EAEA,YAAY,OAAO,OAAO,IAAI;AAC5B,WAAO;AAAA,EACT;AAAA,EAEA,KAAK,OAAO,WAAW,IAAI;AACzB,WAAO;AAAA,EACT;AACF;AAEA,IAAO,gBAAQ;",
4
+ "sourcesContent": ["class WatchablePromise<T> extends Promise<T> {\n #settled = false;\n #state: \"pending\" | \"fulfilled\" | \"rejected\" = \"pending\";\n #value: T | any;\n\n constructor(\n executor: (\n resolve: (value: T | PromiseLike<T>) => void,\n reject: (reason?: any) => void,\n ) => void,\n ) {\n let resolve: (value: T | PromiseLike<T>) => void;\n let reject: (reason?: any) => void;\n\n super((res, rej) => {\n resolve = res;\n reject = rej;\n });\n\n this.then(\n (value) => {\n this.#state = \"fulfilled\";\n this.#value = value;\n this.#settled = true;\n },\n (reason) => {\n this.#state = \"rejected\";\n this.#value = reason;\n this.#settled = true;\n },\n );\n\n // @ts-ignore\n executor(resolve, reject);\n }\n\n get settled() {\n return this.#settled;\n }\n\n get state() {\n return this.#state;\n }\n\n get value() {\n return this.#value;\n }\n\n static from<T>(existingPromise: Promise<T>): WatchablePromise<T> {\n return new WatchablePromise<T>((resolve, reject) => {\n existingPromise.then(resolve, reject);\n });\n }\n\n static withResolvers<T>() {\n let resolve: (value: T | PromiseLike<T>) => void;\n let reject: (reason?: any) => void;\n const promise = new WatchablePromise<T>((res, rej) => {\n resolve = res;\n reject = rej;\n });\n // @ts-ignore\n return { promise, resolve, reject };\n }\n\n static get [Symbol.species]() {\n return Promise;\n }\n\n get [Symbol.toStringTag]() {\n return \"WatchablePromise\";\n }\n}\n\nexport default WatchablePromise;\n"],
5
+ "mappings": ";AAAA,IAAM,mBAAN,MAAM,0BAA4B,QAAW;AAAA,EAC3C,WAAW;AAAA,EACX,SAA+C;AAAA,EAC/C;AAAA,EAEA,YACE,UAIA;AACA,QAAI;AACJ,QAAI;AAEJ,UAAM,CAAC,KAAK,QAAQ;AAClB,gBAAU;AACV,eAAS;AAAA,IACX,CAAC;AAED,SAAK;AAAA,MACH,CAAC,UAAU;AACT,aAAK,SAAS;AACd,aAAK,SAAS;AACd,aAAK,WAAW;AAAA,MAClB;AAAA,MACA,CAAC,WAAW;AACV,aAAK,SAAS;AACd,aAAK,SAAS;AACd,aAAK,WAAW;AAAA,MAClB;AAAA,IACF;AAGA,aAAS,SAAS,MAAM;AAAA,EAC1B;AAAA,EAEA,IAAI,UAAU;AACZ,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,QAAQ;AACV,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,QAAQ;AACV,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,OAAO,KAAQ,iBAAkD;AAC/D,WAAO,IAAI,kBAAoB,CAAC,SAAS,WAAW;AAClD,sBAAgB,KAAK,SAAS,MAAM;AAAA,IACtC,CAAC;AAAA,EACH;AAAA,EAEA,OAAO,gBAAmB;AACxB,QAAI;AACJ,QAAI;AACJ,UAAM,UAAU,IAAI,kBAAoB,CAAC,KAAK,QAAQ;AACpD,gBAAU;AACV,eAAS;AAAA,IACX,CAAC;AAED,WAAO,EAAE,SAAS,SAAS,OAAO;AAAA,EACpC;AAAA,EAEA,YAAY,OAAO,OAAO,IAAI;AAC5B,WAAO;AAAA,EACT;AAAA,EAEA,KAAK,OAAO,WAAW,IAAI;AACzB,WAAO;AAAA,EACT;AACF;AAEA,IAAO,gBAAQ;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "watchable-promise",
3
- "version": "1.0.0",
4
- "description": "A Promise whose status you can read",
3
+ "version": "2.0.1",
4
+ "description": "A Promise whose state and value you can read",
5
5
  "type": "module",
6
6
  "module": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -18,8 +18,7 @@
18
18
  "start": "echo 'There is nothing to start. I think you may just want to run the tests instead' && exit 1",
19
19
  "test": "vitest",
20
20
  "test-once": "vitest run",
21
- "prepack": "npm run require-pristine-tree && npm run test-once",
22
- "prepublish": "npm run build",
21
+ "prepack": "npm run require-pristine-tree && npm run test-once && npm run build",
23
22
  "preversion": "npm run require-pristine-tree && npm run test-once",
24
23
  "prepare": "husky install",
25
24
  "require-pristine-tree": "exit $(git status --porcelain | wc -l)"
@@ -30,7 +29,7 @@
30
29
  },
31
30
  "keywords": [
32
31
  "promises",
33
- "status",
32
+ "state",
34
33
  "watchable"
35
34
  ],
36
35
  "author": {