more-proms 1.1.1 → 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
@@ -55,21 +55,31 @@ promRes()
55
55
 
56
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
57
 
58
- Nested cancellation are also supported. More specific: where nested promises created by `then` or `catch` methods are cancelled when the parent promise is cancelled.
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
+
59
62
 
60
63
  ```ts
61
64
  import { CancelAblePromise } from "more-proms"
62
65
 
66
+ let timeout
63
67
  const p = new CancelAblePromise<string>((resolve, reject) => {
64
- setTimeout(() => {
68
+ timeout = setTimeout(() => {
65
69
  resolve("Hello, world!")
66
70
  }, 1000)
71
+ }, () => {
72
+ console.log("cancelled")
73
+ clearTimeout(timeout)
67
74
  })
68
75
 
76
+
77
+ // later
78
+
69
79
  p.cancel()
70
80
  ```
71
81
 
72
- A nested example: Note how `p1` is cancelled before `p2` resolve, hence only the `p1` will resolve, the `p2` will never resolve.
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.
73
83
 
74
84
  ```ts
75
85
  const p1 = new CancelAblePromise<string>((resolve, reject) => {
@@ -105,17 +115,46 @@ import { latestLatent } from "more-proms"
105
115
  const showPopup = latestLatent(async () => {
106
116
  element.css({display: "block"})
107
117
  await element.animate({opacity: 1})
108
- await waitForCloseInput()
118
+ await closeButton.waitForClick()
109
119
  await element.animate({opacity: 0})
110
120
  })
111
121
 
112
- showPopup().then(() => {
113
- element.css({display: "none"})
122
+
123
+ // later
124
+
125
+ showButton.on("click", () => {
126
+ showPopup().then(() => {
127
+ element.css({display: "none"})
128
+ })
114
129
  })
130
+
115
131
  ```
116
132
 
117
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).
118
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
+
119
158
 
120
159
  ## Contribute
121
160