more-proms 1.1.0 → 1.2.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/README.md CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  A collection of additional promise extending classes. Including a (from the outside) ResablePromise, CancelAblePromise and a latestLatent utility function.
4
4
 
5
- > Please note that More proms is currently under development and not yet suited for production
6
-
7
5
  ## Installation
8
6
 
9
7
  ```shell
@@ -12,14 +10,152 @@ A collection of additional promise extending classes. Including a (from the outs
12
10
 
13
11
  ## Usage
14
12
 
13
+ ### SettledPromise
14
+
15
+ `SettledPromise` is a subclass of `Promise` that adds a `settled` property and an `onSettled` promise. The `settled` property indicates whether the promise has settled (resolved or rejected), and the `onSettled` promise resolves when the `SettledPromise` settles.
16
+
17
+ ```ts
18
+ import { SettledPromise } from "more-proms"
19
+
20
+ const promise = new SettledPromise((resolve, reject) => {
21
+ setTimeout(() => {
22
+ resolve("Hello, world!")
23
+ }, 1000)
24
+ })
25
+
26
+ console.log(promise.settled) // false
27
+
28
+ promise.onSettled.then(() => {
29
+ console.log(promise.settled) // true
30
+ })
31
+ ```
32
+
33
+ ### ResablePromise
34
+
35
+ `ResablePromise` is a subclass of `SettledPromise` that adds `res` and `rej` methods for resolving and rejecting the promise on the fly as consumer. This is only for convenience, as I see myself doing this (see the second example below) a lot. And this provides type safety without effort.
36
+
37
+ ```ts
38
+ import { ResablePromise } from "more-proms"
39
+
40
+ const prom = new ResablePromise()
41
+ // later...
42
+ prom.res()
43
+ ```
44
+
45
+ So you dont have to do this:
46
+
47
+ ```ts
48
+ let promRes
49
+ const prom = new Promise(res => promRes = res)
50
+ // later ...
51
+ promRes()
52
+ ```
53
+
54
+ ### CancelAblePromise
55
+
56
+ `CancelAblePromise` is a subclass of `SettledPromise` that adds cancellation support. It has a `cancel` method for the consumer that can be used to cancel the promise. A canceled promise will never resolve nor will it reject.
57
+
58
+ The promise provider can provide a callback will only ever be called once, and wont be called after resolvement or rejection. This callback can be used to e.g. cancel an ongoing animation, or network request.
59
+
60
+ Note how in this example the clearance of the timeout will have no effect, as a canceled promise wont resolve nor reject even if resolve is called after the timeout finishes. But the two example use cases from above could actually do something useful in the cancel callback.
61
+
62
+
63
+ ```ts
64
+ import { CancelAblePromise } from "more-proms"
65
+
66
+ let timeout
67
+ const p = new CancelAblePromise<string>((resolve, reject) => {
68
+ timeout = setTimeout(() => {
69
+ resolve("Hello, world!")
70
+ }, 1000)
71
+ }, () => {
72
+ console.log("cancelled")
73
+ clearTimeout(timeout)
74
+ })
75
+
15
76
 
77
+ // later
78
+
79
+ p.cancel()
80
+ ```
81
+
82
+ Nested cancellation are also supported. More specific: where nested promises created by `then` or `catch` methods are cancelled when the parent promise is cancelled. A nested example follows below. Note how `p1` is cancelled before `p2` resolve, hence only the `p1` will resolve, the `p2` will never resolve.
16
83
 
17
84
  ```ts
18
- import moreProms from "more-proms"
85
+ const p1 = new CancelAblePromise<string>((resolve, reject) => {
86
+ setTimeout(() => {
87
+ resolve("Hello, world")
88
+ }, 1000)
89
+ })
90
+
91
+ const p2 = p1.then(async (q) => {
92
+ console.log(1)
93
+ await delay(1000)
94
+ return q + "!"
95
+ })
19
96
 
20
- moreProms()
97
+ p2.then(() => {
98
+ console.log(2)
99
+ })
100
+
101
+ delay(1500, () => {
102
+ p1.cancel()
103
+ })
21
104
  ```
22
105
 
106
+ ### latestLatent
107
+
108
+ `latestLatent` is a function that takes a callback function and returns a similar function (acting as the given one) that only executes the latest callback and cancels previous callbacks. This is useful for scenarios where you have asynchronous operations that may be triggered multiple times, but you only want to process the result of the latest operation.
109
+
110
+ A common use case would be a cleanup after an animation that should only be executed when no other animation has been triggered in the meantime.
111
+
112
+ ```ts
113
+ import { latestLatent } from "more-proms"
114
+
115
+ const showPopup = latestLatent(async () => {
116
+ element.css({display: "block"})
117
+ await element.animate({opacity: 1})
118
+ await closeButton.waitForClick()
119
+ await element.animate({opacity: 0})
120
+ })
121
+
122
+
123
+ // later
124
+
125
+ showButton.on("click", () => {
126
+ showPopup().then(() => {
127
+ element.css({display: "none"})
128
+ })
129
+ })
130
+
131
+ ```
132
+
133
+ This way you can be sure that the popup doesnt get `display: none`, when the user opens it again before it has been fully closed (the animation finishes).
134
+
135
+ You may have noticed that the location in your code where you want to `showPopup()` may have a different concern than ensuring that the popupElement is properly hidden. So, to keep the concerns where they belong, you can chain then calls directly on the showPopup provider (where it is declared).
136
+
137
+ ```ts
138
+ import { latestLatent } from "more-proms"
139
+
140
+ const showPopup = latestLatent(async () => {
141
+ element.css({display: "block"})
142
+ await element.animate({opacity: 1})
143
+ await closeButton.waitForClick()
144
+ await element.animate({opacity: 0})
145
+ }).then(() => {
146
+ element.css({display: "none"})
147
+ })
148
+
149
+
150
+ // later
151
+
152
+ showButton.on("click", () => {
153
+ showPopup()
154
+ })
155
+ ```
156
+
157
+
158
+
23
159
  ## Contribute
24
160
 
25
- All feedback is appreciated. Create a pull request or write an issue.
161
+ All feedback is appreciated. Create a pull request or write an issue.