@ndriadev/futurable 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.
- package/LICENSE +21 -0
- package/README.md +536 -0
- package/dist/futurable.cjs +1 -0
- package/dist/futurable.mjs +260 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.d.ts.map +1 -0
- package/package.json +93 -0
- package/scripts/preInstall.js +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Andrea Cosentino
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
|
|
2
|
+
<h1 align="center">Futurable</h1>
|
|
3
|
+
<p align="center">
|
|
4
|
+
Power up for Javascript's Promise API with cancellation support and more.
|
|
5
|
+
</p>
|
|
6
|
+
<p align="center">
|
|
7
|
+
<a href="https://www.npmjs.com/package/futurable">
|
|
8
|
+
<img src="https://img.shields.io/npm/v/futurable?color=orange&label=" alt="version" />
|
|
9
|
+
</a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Summary
|
|
14
|
+
- [ToDo](#TODO)
|
|
15
|
+
- [Introduction](#introduction)
|
|
16
|
+
- [Installation](#Installation)
|
|
17
|
+
- [Usage](#Usage)
|
|
18
|
+
- [Use-case](#Use-case)
|
|
19
|
+
- [React](#React)
|
|
20
|
+
- [API](#API)
|
|
21
|
+
- [constructor](#constructor)
|
|
22
|
+
- [cancel](#cancel)
|
|
23
|
+
- [onCancel](#onCancel)
|
|
24
|
+
- [sleep](#sleep)
|
|
25
|
+
- [delay](#delay)
|
|
26
|
+
- [fetch](#fetch)
|
|
27
|
+
- [promisify](#promisify)
|
|
28
|
+
- [futurizable](#futurizable)
|
|
29
|
+
- [Futurable.onCancel](#Futurable.onCancel)
|
|
30
|
+
- [Futurable.sleep](#Futurable.sleep)
|
|
31
|
+
- [Futurable.delay](#Futurable.delay)
|
|
32
|
+
- [Futurable.fetch](#Futurable.fetch)
|
|
33
|
+
- [Futurable.futurizable](#Futurable.futurizable)
|
|
34
|
+
- [Futurable.all](#Futurable.all)
|
|
35
|
+
- [Futurable.allSettled](Futurable.allSettled)
|
|
36
|
+
- [Futurable.race](#Futurable.race)
|
|
37
|
+
- [Futurable.any](#Futurable.any)
|
|
38
|
+
- [License](#License)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# ToDo
|
|
42
|
+
- Think about the possibility of making a static method that returns an object with the futurable, resolve, reject, utils properties inside to be used as done for usePromiser.
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# Introduction
|
|
46
|
+
Futurable is a library that extends Javascript's Promise API, adding a number of useful features and with support for Typescirpt. It can be used on both browser and node.
|
|
47
|
+
|
|
48
|
+
Often it happens where to develop a feature using promises that covers a particular need. Often there is a need to delay execution, or even to cancel what is in progress. Javascript's Promise API doesn't offer an immediate way to do this, so we are forced to implement the code ourselves that does what we need. The purpose of this library is to provide these features ready to use, without the user having to think about anything else.
|
|
49
|
+
|
|
50
|
+
:warning: If you intend to use the library in node, for versions lower than **17.5.0** it is necessary to install the *node-fetch* library, since the native support for the Fetch API was introduced by this version.
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
```bash
|
|
54
|
+
|
|
55
|
+
npm install futurable # or yarn add futurable or pnpm add futurable
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# Usage
|
|
61
|
+
The library supports both ESM and CJS formats, so it can be used as follows:
|
|
62
|
+
```javascript
|
|
63
|
+
import Futurable from 'futurable'; // ok
|
|
64
|
+
|
|
65
|
+
const { Futurable } = require('futurable'); // ok
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Use-case
|
|
69
|
+
### React
|
|
70
|
+
Thanks to the use of this library, there is a simple and effective way to be able to cancel an Api request executed in a useEffect which, due to the Strict Mode, is executed twice:
|
|
71
|
+
```jsx
|
|
72
|
+
export default function Component() {
|
|
73
|
+
//...code
|
|
74
|
+
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
const f;
|
|
77
|
+
function callApi() {
|
|
78
|
+
f = Futurable
|
|
79
|
+
.fetch("https://jsonplaceholder.typicode.com/todos/2")
|
|
80
|
+
.onAbort(() => console.log("aborted"))
|
|
81
|
+
.then(resp => resp.json())
|
|
82
|
+
.then(setTodo);
|
|
83
|
+
}
|
|
84
|
+
callApi();
|
|
85
|
+
return () => {
|
|
86
|
+
f && f.cancel();
|
|
87
|
+
}
|
|
88
|
+
},[])
|
|
89
|
+
|
|
90
|
+
//OR
|
|
91
|
+
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
const controller = new AbortController();
|
|
94
|
+
Futurable
|
|
95
|
+
.fetch(
|
|
96
|
+
"https://jsonplaceholder.typicode.com/todos/2",
|
|
97
|
+
{
|
|
98
|
+
signal: controller.signal
|
|
99
|
+
}
|
|
100
|
+
)
|
|
101
|
+
.then(resp => resp.json())
|
|
102
|
+
.then(setTodo);
|
|
103
|
+
|
|
104
|
+
return () => {
|
|
105
|
+
controller.abort();
|
|
106
|
+
}
|
|
107
|
+
},[])
|
|
108
|
+
|
|
109
|
+
//...code
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
# API
|
|
114
|
+
The methods implemented, excluding those that are by nature static can be used:
|
|
115
|
+
- During the construction of the futurable using the ***new*** operator;
|
|
116
|
+
- In the chain-style ***promise chaining***.
|
|
117
|
+
|
|
118
|
+
They are the following:
|
|
119
|
+
- [cancel](#cancel)
|
|
120
|
+
- [onCancel](#onCancel)
|
|
121
|
+
- [sleep](#sleep)
|
|
122
|
+
- [delay](#delay)
|
|
123
|
+
- [fetch](#fetch)
|
|
124
|
+
- [promisify](#promisify)
|
|
125
|
+
- [futurizable](#futurizable)
|
|
126
|
+
- [Futurable.onCancel](#Futurable.onCancel)
|
|
127
|
+
- [Futurable.sleep](#Futurable.sleep)
|
|
128
|
+
- [Futurable.delay](#Futurable.delay)
|
|
129
|
+
- [Futurable.fetch](#Futurable.fetch)
|
|
130
|
+
- [Futurable.futurizable](#Futurable.futurizable)
|
|
131
|
+
- [Futurable.all](#Futurable.all)
|
|
132
|
+
- [Futurable.allSettled](Futurable.allSettled)
|
|
133
|
+
- [Futurable.race](#Futurable.race)
|
|
134
|
+
- [Futurable.any](#Futurable.any)
|
|
135
|
+
|
|
136
|
+
### constructor(executor: FuturableExecutor, signal?: AbortSignal)
|
|
137
|
+
Futurable is instantiable like a classic Promise.
|
|
138
|
+
```javascript
|
|
139
|
+
//Javascript Promise
|
|
140
|
+
|
|
141
|
+
const promise = new Promise((resolve, reject) => {
|
|
142
|
+
const data = /*..async operations or other..*/
|
|
143
|
+
resolve(data);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
//Futurable
|
|
147
|
+
import Futurable from 'futurable';
|
|
148
|
+
|
|
149
|
+
const futurable = new Futurable((resolve, reject) => {
|
|
150
|
+
const data = /*..async operations or other..*/
|
|
151
|
+
resolve(data);
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
But it provides two more statements:
|
|
155
|
+
|
|
156
|
+
1. Its constructor can receive a second parameter *signal*, an *AbortSignal*, usable to cancel the promise from the outside.
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
159
|
+
const controller = new AbortController();
|
|
160
|
+
|
|
161
|
+
const futurable = new Futurable((resolve, reject) => {
|
|
162
|
+
const data = /*..async operations or other..*/
|
|
163
|
+
resolve(data);
|
|
164
|
+
}, controller.signal);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
2. The executor function passed to the promise receives a third parameter, *utils*, optional.
|
|
168
|
+
|
|
169
|
+
```javascript
|
|
170
|
+
const controller = new AbortController();
|
|
171
|
+
|
|
172
|
+
const futurable = new Futurable((resolve, reject, utils) => {
|
|
173
|
+
const data = /*..async operations or other..*/
|
|
174
|
+
resolve(data);
|
|
175
|
+
});
|
|
176
|
+
```
|
|
177
|
+
Utils is an object with the following properties which mirror the methods described in the usage section and which will be described below:
|
|
178
|
+
- cancel;
|
|
179
|
+
- onCancel:
|
|
180
|
+
- delay;
|
|
181
|
+
- sleep;
|
|
182
|
+
- fetch;
|
|
183
|
+
- futurizable.
|
|
184
|
+
|
|
185
|
+
In addition is has:
|
|
186
|
+
- signal: internal futurable signal;
|
|
187
|
+
|
|
188
|
+
### cancel()
|
|
189
|
+
If invoked, it cancel the futurable if it is to be executed or if it is still executing.
|
|
190
|
+
|
|
191
|
+
*Example*
|
|
192
|
+
```javascript
|
|
193
|
+
function asynchronousOperation() {
|
|
194
|
+
return new Futurable((res, rej) => {
|
|
195
|
+
// asynchornous code..
|
|
196
|
+
resolve(true);
|
|
197
|
+
});
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
//...code
|
|
201
|
+
|
|
202
|
+
const futurable = asynchronousOperation();
|
|
203
|
+
futurable.then(value => {
|
|
204
|
+
//DO anything
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
//...code
|
|
208
|
+
|
|
209
|
+
futurable.cancel();
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### onCancel(cb: callback)
|
|
213
|
+
If it is invoked, when the futurable is cancelled, it executes the callback passed as a parameter.
|
|
214
|
+
|
|
215
|
+
*Example*
|
|
216
|
+
```javascript
|
|
217
|
+
const futurable = new Futurable((resolve, reject, utils) => {
|
|
218
|
+
utils.onCancel(() => console.log("Futurable cancelled"));
|
|
219
|
+
const data = /*..async operations or other..*/
|
|
220
|
+
resolve(data);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
//...code
|
|
224
|
+
|
|
225
|
+
futurable.cancel();
|
|
226
|
+
|
|
227
|
+
//OR
|
|
228
|
+
|
|
229
|
+
const futurable = new Futurable((res, rej) => {
|
|
230
|
+
// asynchornous code..
|
|
231
|
+
resolve(true);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
//...code
|
|
235
|
+
|
|
236
|
+
futurable
|
|
237
|
+
.onCancel(() => console.log("Futurable cancelled"))
|
|
238
|
+
.then(val => .......);
|
|
239
|
+
|
|
240
|
+
//...code
|
|
241
|
+
|
|
242
|
+
futurable.cancel();
|
|
243
|
+
```
|
|
244
|
+
```bash
|
|
245
|
+
Output: Futurable cancelled
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### sleep(timer: number)
|
|
249
|
+
Waits for timer parameter (in milliseconds) before returning the value.
|
|
250
|
+
|
|
251
|
+
*Example*
|
|
252
|
+
```javascript
|
|
253
|
+
const futurable = new Futurable((resolve, reject, utils) => {
|
|
254
|
+
const data = /*..async operations or other..*/
|
|
255
|
+
utils.sleep(3000);
|
|
256
|
+
resolve(data);
|
|
257
|
+
});
|
|
258
|
+
//...code
|
|
259
|
+
|
|
260
|
+
//OR
|
|
261
|
+
|
|
262
|
+
const futurable = new Futurable((res, rej) => {
|
|
263
|
+
// asynchornous code..
|
|
264
|
+
resolve(true);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
//...code
|
|
268
|
+
|
|
269
|
+
futurable
|
|
270
|
+
.sleep(3000)
|
|
271
|
+
.then(val => .......);
|
|
272
|
+
|
|
273
|
+
//...code
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### delay(cb: callback, timer: number)
|
|
277
|
+
Waits for timer parameter (in milliseconds), then executes callback with the futurable value and returns the result obtained from the invocation.
|
|
278
|
+
*Example*
|
|
279
|
+
```javascript
|
|
280
|
+
const futurable = new Futurable((resolve, reject, utils) => {
|
|
281
|
+
const data = /*..async operations or other..*/
|
|
282
|
+
utils.delay(()=>console.log("delayed"), 3000);
|
|
283
|
+
resolve(data);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
//...code
|
|
287
|
+
|
|
288
|
+
//OR
|
|
289
|
+
|
|
290
|
+
const futurable = new Futurable((res, rej) => {
|
|
291
|
+
// asynchornous code..
|
|
292
|
+
resolve(true);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
//...code
|
|
296
|
+
|
|
297
|
+
futurable
|
|
298
|
+
.delay((val)=> {
|
|
299
|
+
console.log("delayed val", val);
|
|
300
|
+
return val;
|
|
301
|
+
},3000)
|
|
302
|
+
.then(val => .......);
|
|
303
|
+
|
|
304
|
+
//...code
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### fetch(url: string | (val => string), opts: object | RequestInit)
|
|
308
|
+
Extension of the fetch API with cancellation support. Url parameter can be a string or a function with receive value from futurable chaining as paremeter
|
|
309
|
+
|
|
310
|
+
*Example*
|
|
311
|
+
```javascript
|
|
312
|
+
const futurable = new Futurable((resolve, reject, utils) => {
|
|
313
|
+
utils.fetch(/*string url to fetch..*/)
|
|
314
|
+
.then(val => resolve(val))
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
//...code
|
|
318
|
+
|
|
319
|
+
//OR
|
|
320
|
+
|
|
321
|
+
const futurable = new Futurable((res, rej) => {
|
|
322
|
+
// asynchornous code..
|
|
323
|
+
resolve(true);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
//...code
|
|
327
|
+
|
|
328
|
+
futurable
|
|
329
|
+
.fetch(/*url to fetch..*/)
|
|
330
|
+
.then(val => .......);
|
|
331
|
+
|
|
332
|
+
//OR
|
|
333
|
+
futurable
|
|
334
|
+
.then(val => "https://...")
|
|
335
|
+
.fetch((val /* val came from previous then*/) => ..., ..)
|
|
336
|
+
|
|
337
|
+
//...code
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
<!---
|
|
341
|
+
### promisify()
|
|
342
|
+
Transforms the futurable into a normal promise in order to be able to use the async/await syntax but keeping possibility to cancel futurable until its invocation.
|
|
343
|
+
|
|
344
|
+
*Example*
|
|
345
|
+
```javascript
|
|
346
|
+
async function op() {
|
|
347
|
+
...
|
|
348
|
+
...
|
|
349
|
+
|
|
350
|
+
await Futurable.sleep(3000).promisify();
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
--->
|
|
354
|
+
### futurizable(promise: Promise | (val => Promise))
|
|
355
|
+
Takes a promise and transforms it into a futurizable. Promise can be also a function that receives value from futurable chaining as parameter.
|
|
356
|
+
|
|
357
|
+
*Example*
|
|
358
|
+
```javascript
|
|
359
|
+
const futurable = new Futurable((resolve, reject, utils) => {
|
|
360
|
+
utils.futurizable(new Promise(res => {
|
|
361
|
+
//asynchronous code
|
|
362
|
+
res(data);
|
|
363
|
+
}))
|
|
364
|
+
.then(val => resolve(val))
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
//...code
|
|
368
|
+
|
|
369
|
+
//OR
|
|
370
|
+
|
|
371
|
+
const futurable = new Futurable((res, rej) => {
|
|
372
|
+
// asynchornous code..
|
|
373
|
+
resolve(true);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
//...code
|
|
377
|
+
|
|
378
|
+
futurable
|
|
379
|
+
.futurizable(/*promise to futurizable*/)
|
|
380
|
+
.then(val => .......);
|
|
381
|
+
|
|
382
|
+
//OR
|
|
383
|
+
futurable
|
|
384
|
+
.then(val => 3)
|
|
385
|
+
.futurizable((val /* val is 3 */) => new Promise(/*...*/) /*promise to futurizable*/, ..)
|
|
386
|
+
|
|
387
|
+
//...code
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### Futurable.onCancel(cb: callback | {cb: callback, signal?: AbortSignal})
|
|
391
|
+
OnCancel static method. It accepts a callback or a object with cb property and an optional signal.
|
|
392
|
+
|
|
393
|
+
*Example*
|
|
394
|
+
```javascript
|
|
395
|
+
const controller = new AbortController();
|
|
396
|
+
|
|
397
|
+
//...code
|
|
398
|
+
Futurable.onCancel({
|
|
399
|
+
cb: ()=>console.log("Cancelled"),
|
|
400
|
+
signal: controller.signal
|
|
401
|
+
});
|
|
402
|
+
//...code
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### Futurable.sleep(timer: number | {timer: number, signal?: AbortSignal})
|
|
406
|
+
Sleep static method. It accepts a timer or a object with timer property and an optional signal.
|
|
407
|
+
|
|
408
|
+
*Example*
|
|
409
|
+
```javascript
|
|
410
|
+
//...code
|
|
411
|
+
|
|
412
|
+
Futurable.sleep({
|
|
413
|
+
timer: 3000,
|
|
414
|
+
signal: signal
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
//...code
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### Futurable.delay({cb: callback, timer: number, signal?: AbortSignal})
|
|
421
|
+
Delay static method. It accepts a object with timer and cb properties and an optional signal property.
|
|
422
|
+
|
|
423
|
+
*Example*
|
|
424
|
+
```javascript
|
|
425
|
+
const controller = new AbortController();
|
|
426
|
+
//...code
|
|
427
|
+
|
|
428
|
+
Futurable.delay({
|
|
429
|
+
cb: ()=>console.log("Cancelled"),
|
|
430
|
+
timer: 3000
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
//...code
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
### Futurable.fetch(url: string, opts: object | RequestInit)
|
|
437
|
+
Fetch static method.
|
|
438
|
+
|
|
439
|
+
*Example*
|
|
440
|
+
```javascript
|
|
441
|
+
//...code
|
|
442
|
+
|
|
443
|
+
Futurable.fetch(/*url string..*/, {method: "POST"});
|
|
444
|
+
|
|
445
|
+
//...code
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
### Futurable.futurizable({promise: Promise, signal: AbortSignal})
|
|
449
|
+
Futurizable static method.
|
|
450
|
+
|
|
451
|
+
*Example*
|
|
452
|
+
```javascript
|
|
453
|
+
const controller = new AbortController();
|
|
454
|
+
//...code
|
|
455
|
+
|
|
456
|
+
Futurable.futurizable({promise: /*promise to futurizable*/, signal: controller.signal});
|
|
457
|
+
|
|
458
|
+
//...code
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
### Futurable.all(iterable: FuturableIterable[], signal?: AbortSignal)
|
|
462
|
+
Extension of the static method all with cancellation support.
|
|
463
|
+
|
|
464
|
+
*Example*
|
|
465
|
+
```javascript
|
|
466
|
+
const controller = new AbortController();
|
|
467
|
+
|
|
468
|
+
//...code
|
|
469
|
+
|
|
470
|
+
Futurable.all([
|
|
471
|
+
1,
|
|
472
|
+
Futurable.resolve(true),
|
|
473
|
+
new Futurable/*...*/
|
|
474
|
+
], controller.signal);
|
|
475
|
+
|
|
476
|
+
//...code
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
### Futurable.allSettled(iterable: FuturableIterable[], signal?: AbortSignal)
|
|
480
|
+
Extension of the static method allSettled with cancellation support.
|
|
481
|
+
|
|
482
|
+
*Example*
|
|
483
|
+
```javascript
|
|
484
|
+
const controller = new AbortController();
|
|
485
|
+
|
|
486
|
+
//...code
|
|
487
|
+
|
|
488
|
+
Futurable.allSettled([
|
|
489
|
+
1,
|
|
490
|
+
Futurable.resolve(true),
|
|
491
|
+
new Futurable/*...*/
|
|
492
|
+
], controller.signal);
|
|
493
|
+
|
|
494
|
+
//...code
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
### Futurable.any(iterable: FuturableIterable[], signal?: AbortSignal)
|
|
498
|
+
Extension of the static method any with cancellation support.
|
|
499
|
+
|
|
500
|
+
*Example*
|
|
501
|
+
```javascript
|
|
502
|
+
const controller = new AbortController();
|
|
503
|
+
//...code
|
|
504
|
+
|
|
505
|
+
Futurable.any([
|
|
506
|
+
1,
|
|
507
|
+
Futurable.resolve(true),
|
|
508
|
+
new Futurable/*...*/
|
|
509
|
+
], controller.signal);
|
|
510
|
+
|
|
511
|
+
//...code
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
### Futurable.race(iterable: FuturableIterable[], signal?: AbortSignal)
|
|
515
|
+
Extension of the static method race with cancellation support.
|
|
516
|
+
|
|
517
|
+
*Example*
|
|
518
|
+
```javascript
|
|
519
|
+
const controller = new AbortController();
|
|
520
|
+
//...code
|
|
521
|
+
|
|
522
|
+
Futurable.race([
|
|
523
|
+
1,
|
|
524
|
+
Futurable.resolve(true),
|
|
525
|
+
new Futurable/*...*/
|
|
526
|
+
], controller.signal);
|
|
527
|
+
|
|
528
|
+
//...code
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
## License
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
Futurable is licensed under a [MIT License](./LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var I=(a,f,e)=>{if(!f.has(a))throw TypeError("Cannot "+e)};var l=(a,f,e)=>(I(a,f,"read from private field"),e?e.call(a):f.get(a)),C=(a,f,e)=>{if(f.has(a))throw TypeError("Cannot add the same private member more than once");f instanceof WeakSet?f.add(a):f.set(a,e)},p=(a,f,e,t)=>(I(a,f,"write to private field"),t?t.call(a,e):f.set(a,e),e);var w=(a,f,e)=>(I(a,f,"access private method"),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var x=(a=>(a.PENDING="pending",a.FULFILLED="fulfilled",a.REJECTED="rejected",a))(x||{}),u,d,T,S,L,v,P;const i=class extends Promise{constructor(e,t){const n=t?null:new AbortController,r=t||n.signal,s=[],o=()=>{for(const h of s)clearTimeout(h)};let c;const y={signal:r,cancel:()=>{var h;return(h=l(this,u))==null?void 0:h.abort()},onCancel:h=>{c=h},delay:(h,b)=>new i(m=>{s.push(setTimeout(()=>{m(h())},b))},r),sleep:h=>y.delay(()=>{},h),fetch:(h,b)=>new i((m,N)=>{fetch(h,{...b||{},signal:r}).then(E=>m(E)).catch(E=>{E.name!=="AbortError"&&N(E)})},r),futurizable:h=>new i((b,m)=>{h.then(b).catch(m)},r)};let j="pending";const A=new Promise((h,b)=>{if(r.aborted){o(),j==="pending"&&c&&c();return}else{const m=typeof r.onabort=="function"?r.onabort:()=>{};r.onabort=()=>{m(),o(),j==="pending"&&c&&c()},e(D=>{j="fulfilled",h(D)},D=>{j="rejected",b(D)},y)}});super((h,b)=>{A.then(m=>h(m)).catch(b)});C(this,S);C(this,u,void 0);C(this,d,void 0);C(this,T,void 0);p(this,u,n),p(this,d,r),p(this,T,s)}static get[Symbol.species](){return this}get[Symbol.toStringTag](){return"Futurable"}get signal(){return l(this,d)}then(e,t){let n,r;const s=new i((o,c)=>{n=o,r=c},l(this,d));return p(s,u,l(this,u)),super.then(o=>{var c;if((c=l(this,d))!=null&&c.aborted){w(this,S,L).call(this);return}try{n(e?e(o):o)}catch(g){r(g)}},o=>{var c;if((c=l(this,d))!=null&&c.aborted){w(this,S,L).call(this);return}try{t?n(t(o)):r(o)}catch(g){r(g)}}),s}catch(e){return this.then(null,e)}finally(e){return this.then(()=>{e()},()=>{e()})}cancel(){var e,t;!((e=l(this,d))!=null&&e.aborted)&&((t=l(this,u))==null||t.abort())}delay(e,t){let n,r;const s=new i((o,c)=>{n=o,r=c},l(this,d));return p(s,u,l(this,u)),this.then(o=>{l(this,T).push(setTimeout(()=>n(e(o)),t))},o=>{r(o)}),s}sleep(e){return this.delay(t=>t,e)}fetch(e,t){let n,r;const s=new i((o,c)=>{n=o,r=c},l(this,d));return p(s,u,l(this,u)),this.then(o=>{const c=typeof e=="function"?e(o):e,g={...typeof t=="function"?t(o):t,signal:l(this,d)};fetch(c,g).then(y=>n(y)).catch(y=>{y.name!=="AbortError"&&r(y)})}),s}onCancel(e){let t,n;const r=new i((s,o,c)=>{c.onCancel(e),t=s,n=o},l(this,d));return p(r,u,l(this,u)),this.then(s=>t(s),s=>n(s)),r}futurizable(e){let t,n;const r=new i((s,o)=>{t=s,n=o},l(this,d));return p(r,u,l(this,u)),this.then(s=>{(typeof e=="function"?e(s):e).then(t).catch(n)}),r}static resolve(e,t){return new i(n=>n(e),t)}static reject(e,t){return new i((n,r)=>r(e),t)}static onCancel({cb:e,signal:t}){return new i((n,r,s)=>{s.onCancel(()=>n(e()))},t)}static delay({cb:e,timer:t,signal:n}){return i.resolve(!0,n).delay(e,t)}static sleep({timer:e,signal:t}){return i.delay({cb:()=>{},timer:e,signal:t})}static fetch(e,t){const n=(t==null?void 0:t.signal)||void 0;return t!=null&&t.signal&&delete t.signal,i.resolve(!0,n).fetch(e,t)}static futurizable({promise:e,signal:t}){return new i((n,r)=>{e.then(n).catch(r)},t)}static all(e,t){var c;const{f:n,resolve:r,reject:s,array:o}=w(c=i,v,P).call(c,e,t);return super.all(o).then(r).catch(s),n}static allSettled(e,t){var c;const{f:n,resolve:r,reject:s,array:o}=w(c=i,v,P).call(c,e,t);return super.allSettled(o).then(r).catch(s),n}static race(e,t){var c;const{f:n,resolve:r,reject:s,array:o}=w(c=i,v,P).call(c,e,t);return super.race(o).then(r).catch(s),n}static any(e,t){var c;const{f:n,resolve:r,reject:s,array:o}=w(c=i,v,P).call(c,e,t);return super.any(o).then(r).catch(s),n}};let z=i;u=new WeakMap,d=new WeakMap,T=new WeakMap,S=new WeakSet,L=function(){for(const e of l(this,T))clearTimeout(e)},v=new WeakSet,P=function(e,t){let n,r;const s=[],o=new i((c,g,y)=>{n=c,r=g,y.onCancel(()=>{for(const j of s)j.signal!==t&&j.cancel()})},t);t||(t=o.signal);for(const c in e)e[c]instanceof i?s.push(e[c]):e[c]instanceof Promise?s.push(new i((g,y)=>{e[c].then(g).catch(y)},t)):s.push(i.resolve(e[c]));return{f:o,resolve:n,reject:r,array:s}},C(z,v);exports.FUTURABLE_STATUS=x;exports.Futurable=z;
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
var I = (a, f, e) => {
|
|
2
|
+
if (!f.has(a))
|
|
3
|
+
throw TypeError("Cannot " + e);
|
|
4
|
+
};
|
|
5
|
+
var l = (a, f, e) => (I(a, f, "read from private field"), e ? e.call(a) : f.get(a)), C = (a, f, e) => {
|
|
6
|
+
if (f.has(a))
|
|
7
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
8
|
+
f instanceof WeakSet ? f.add(a) : f.set(a, e);
|
|
9
|
+
}, w = (a, f, e, t) => (I(a, f, "write to private field"), t ? t.call(a, e) : f.set(a, e), e);
|
|
10
|
+
var j = (a, f, e) => (I(a, f, "access private method"), e);
|
|
11
|
+
var L = /* @__PURE__ */ ((a) => (a.PENDING = "pending", a.FULFILLED = "fulfilled", a.REJECTED = "rejected", a))(L || {}), u, d, E, z, N, v, T;
|
|
12
|
+
const i = class extends Promise {
|
|
13
|
+
constructor(e, t) {
|
|
14
|
+
const n = t ? null : new AbortController(), r = t || n.signal, s = [], o = () => {
|
|
15
|
+
for (const h of s)
|
|
16
|
+
clearTimeout(h);
|
|
17
|
+
};
|
|
18
|
+
let c;
|
|
19
|
+
const y = {
|
|
20
|
+
signal: r,
|
|
21
|
+
cancel: () => {
|
|
22
|
+
var h;
|
|
23
|
+
return (h = l(this, u)) == null ? void 0 : h.abort();
|
|
24
|
+
},
|
|
25
|
+
onCancel: (h) => {
|
|
26
|
+
c = h;
|
|
27
|
+
},
|
|
28
|
+
delay: (h, p) => new i((m) => {
|
|
29
|
+
s.push(setTimeout(() => {
|
|
30
|
+
m(h());
|
|
31
|
+
}, p));
|
|
32
|
+
}, r),
|
|
33
|
+
sleep: (h) => y.delay(() => {
|
|
34
|
+
}, h),
|
|
35
|
+
fetch: (h, p) => new i((m, G) => {
|
|
36
|
+
fetch(h, { ...p || {}, signal: r }).then((P) => m(P)).catch((P) => {
|
|
37
|
+
P.name !== "AbortError" && G(P);
|
|
38
|
+
});
|
|
39
|
+
}, r),
|
|
40
|
+
futurizable: (h) => new i((p, m) => {
|
|
41
|
+
h.then(p).catch(m);
|
|
42
|
+
}, r)
|
|
43
|
+
};
|
|
44
|
+
let b = "pending";
|
|
45
|
+
const J = new Promise((h, p) => {
|
|
46
|
+
if (r.aborted) {
|
|
47
|
+
o(), b === "pending" && c && c();
|
|
48
|
+
return;
|
|
49
|
+
} else {
|
|
50
|
+
const m = typeof r.onabort == "function" ? r.onabort : () => {
|
|
51
|
+
};
|
|
52
|
+
r.onabort = () => {
|
|
53
|
+
m(), o(), b === "pending" && c && c();
|
|
54
|
+
}, e((D) => {
|
|
55
|
+
b = "fulfilled", h(D);
|
|
56
|
+
}, (D) => {
|
|
57
|
+
b = "rejected", p(D);
|
|
58
|
+
}, y);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
super((h, p) => {
|
|
62
|
+
J.then((m) => h(m)).catch(p);
|
|
63
|
+
});
|
|
64
|
+
C(this, z);
|
|
65
|
+
C(this, u, void 0);
|
|
66
|
+
C(this, d, void 0);
|
|
67
|
+
C(this, E, void 0);
|
|
68
|
+
w(this, u, n), w(this, d, r), w(this, E, s);
|
|
69
|
+
}
|
|
70
|
+
static get [Symbol.species]() {
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
get [Symbol.toStringTag]() {
|
|
74
|
+
return "Futurable";
|
|
75
|
+
}
|
|
76
|
+
get signal() {
|
|
77
|
+
return l(this, d);
|
|
78
|
+
}
|
|
79
|
+
then(e, t) {
|
|
80
|
+
let n, r;
|
|
81
|
+
const s = new i((o, c) => {
|
|
82
|
+
n = o, r = c;
|
|
83
|
+
}, l(this, d));
|
|
84
|
+
return w(s, u, l(this, u)), super.then((o) => {
|
|
85
|
+
var c;
|
|
86
|
+
if ((c = l(this, d)) != null && c.aborted) {
|
|
87
|
+
j(this, z, N).call(this);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
n(e ? e(o) : o);
|
|
92
|
+
} catch (g) {
|
|
93
|
+
r(g);
|
|
94
|
+
}
|
|
95
|
+
}, (o) => {
|
|
96
|
+
var c;
|
|
97
|
+
if ((c = l(this, d)) != null && c.aborted) {
|
|
98
|
+
j(this, z, N).call(this);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
t ? n(t(o)) : r(o);
|
|
103
|
+
} catch (g) {
|
|
104
|
+
r(g);
|
|
105
|
+
}
|
|
106
|
+
}), s;
|
|
107
|
+
}
|
|
108
|
+
catch(e) {
|
|
109
|
+
return this.then(null, e);
|
|
110
|
+
}
|
|
111
|
+
finally(e) {
|
|
112
|
+
return this.then(
|
|
113
|
+
() => {
|
|
114
|
+
e();
|
|
115
|
+
},
|
|
116
|
+
() => {
|
|
117
|
+
e();
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
cancel() {
|
|
122
|
+
var e, t;
|
|
123
|
+
!((e = l(this, d)) != null && e.aborted) && ((t = l(this, u)) == null || t.abort());
|
|
124
|
+
}
|
|
125
|
+
delay(e, t) {
|
|
126
|
+
let n, r;
|
|
127
|
+
const s = new i((o, c) => {
|
|
128
|
+
n = o, r = c;
|
|
129
|
+
}, l(this, d));
|
|
130
|
+
return w(s, u, l(this, u)), this.then(
|
|
131
|
+
(o) => {
|
|
132
|
+
l(this, E).push(setTimeout(() => n(e(o)), t));
|
|
133
|
+
},
|
|
134
|
+
(o) => {
|
|
135
|
+
r(o);
|
|
136
|
+
}
|
|
137
|
+
), s;
|
|
138
|
+
}
|
|
139
|
+
sleep(e) {
|
|
140
|
+
return this.delay((t) => t, e);
|
|
141
|
+
}
|
|
142
|
+
fetch(e, t) {
|
|
143
|
+
let n, r;
|
|
144
|
+
const s = new i((o, c) => {
|
|
145
|
+
n = o, r = c;
|
|
146
|
+
}, l(this, d));
|
|
147
|
+
return w(s, u, l(this, u)), this.then((o) => {
|
|
148
|
+
const c = typeof e == "function" ? e(o) : e, g = { ...typeof t == "function" ? t(o) : t, signal: l(this, d) };
|
|
149
|
+
fetch(c, g).then((y) => n(y)).catch((y) => {
|
|
150
|
+
y.name !== "AbortError" && r(y);
|
|
151
|
+
});
|
|
152
|
+
}), s;
|
|
153
|
+
}
|
|
154
|
+
onCancel(e) {
|
|
155
|
+
let t, n;
|
|
156
|
+
const r = new i((s, o, c) => {
|
|
157
|
+
c.onCancel(e), t = s, n = o;
|
|
158
|
+
}, l(this, d));
|
|
159
|
+
return w(r, u, l(this, u)), this.then(
|
|
160
|
+
(s) => t(s),
|
|
161
|
+
(s) => n(s)
|
|
162
|
+
), r;
|
|
163
|
+
}
|
|
164
|
+
// promisify<TResult1 = T, TResult2 = never>(): Promise<TResult1 | TResult2> {
|
|
165
|
+
// return new Promise((res, rej) => {
|
|
166
|
+
// if (this.#signal.aborted) {
|
|
167
|
+
// this.#clearTimeout();
|
|
168
|
+
// return;
|
|
169
|
+
// } else {
|
|
170
|
+
// this.then(
|
|
171
|
+
// val => res(val),
|
|
172
|
+
// reason => rej(reason)
|
|
173
|
+
// );
|
|
174
|
+
// }
|
|
175
|
+
// });
|
|
176
|
+
// }
|
|
177
|
+
futurizable(e) {
|
|
178
|
+
let t, n;
|
|
179
|
+
const r = new i((s, o) => {
|
|
180
|
+
t = s, n = o;
|
|
181
|
+
}, l(this, d));
|
|
182
|
+
return w(r, u, l(this, u)), this.then((s) => {
|
|
183
|
+
(typeof e == "function" ? e(s) : e).then(t).catch(n);
|
|
184
|
+
}), r;
|
|
185
|
+
}
|
|
186
|
+
static resolve(e, t) {
|
|
187
|
+
return new i((n) => n(e), t);
|
|
188
|
+
}
|
|
189
|
+
static reject(e, t) {
|
|
190
|
+
return new i((n, r) => r(e), t);
|
|
191
|
+
}
|
|
192
|
+
static onCancel({ cb: e, signal: t }) {
|
|
193
|
+
return new i((n, r, s) => {
|
|
194
|
+
s.onCancel(() => n(e()));
|
|
195
|
+
}, t);
|
|
196
|
+
}
|
|
197
|
+
static delay({ cb: e, timer: t, signal: n }) {
|
|
198
|
+
return i.resolve(!0, n).delay(e, t);
|
|
199
|
+
}
|
|
200
|
+
static sleep({ timer: e, signal: t }) {
|
|
201
|
+
return i.delay({
|
|
202
|
+
cb: () => {
|
|
203
|
+
},
|
|
204
|
+
timer: e,
|
|
205
|
+
signal: t
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
static fetch(e, t) {
|
|
209
|
+
const n = (t == null ? void 0 : t.signal) || void 0;
|
|
210
|
+
return t != null && t.signal && delete t.signal, i.resolve(!0, n).fetch(e, t);
|
|
211
|
+
}
|
|
212
|
+
static futurizable({ promise: e, signal: t }) {
|
|
213
|
+
return new i((n, r) => {
|
|
214
|
+
e.then(n).catch(r);
|
|
215
|
+
}, t);
|
|
216
|
+
}
|
|
217
|
+
static all(e, t) {
|
|
218
|
+
var c;
|
|
219
|
+
const { f: n, resolve: r, reject: s, array: o } = j(c = i, v, T).call(c, e, t);
|
|
220
|
+
return super.all(o).then(r).catch(s), n;
|
|
221
|
+
}
|
|
222
|
+
static allSettled(e, t) {
|
|
223
|
+
var c;
|
|
224
|
+
const { f: n, resolve: r, reject: s, array: o } = j(c = i, v, T).call(c, e, t);
|
|
225
|
+
return super.allSettled(o).then(r).catch(s), n;
|
|
226
|
+
}
|
|
227
|
+
static race(e, t) {
|
|
228
|
+
var c;
|
|
229
|
+
const { f: n, resolve: r, reject: s, array: o } = j(c = i, v, T).call(c, e, t);
|
|
230
|
+
return super.race(o).then(r).catch(s), n;
|
|
231
|
+
}
|
|
232
|
+
static any(e, t) {
|
|
233
|
+
var c;
|
|
234
|
+
const { f: n, resolve: r, reject: s, array: o } = j(c = i, v, T).call(c, e, t);
|
|
235
|
+
return super.any(o).then(r).catch(s), n;
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
let x = i;
|
|
239
|
+
u = new WeakMap(), d = new WeakMap(), E = new WeakMap(), z = new WeakSet(), N = function() {
|
|
240
|
+
for (const e of l(this, E))
|
|
241
|
+
clearTimeout(e);
|
|
242
|
+
}, v = new WeakSet(), T = function(e, t) {
|
|
243
|
+
let n, r;
|
|
244
|
+
const s = [], o = new i((c, g, y) => {
|
|
245
|
+
n = c, r = g, y.onCancel(() => {
|
|
246
|
+
for (const b of s)
|
|
247
|
+
b.signal !== t && b.cancel();
|
|
248
|
+
});
|
|
249
|
+
}, t);
|
|
250
|
+
t || (t = o.signal);
|
|
251
|
+
for (const c in e)
|
|
252
|
+
e[c] instanceof i ? s.push(e[c]) : e[c] instanceof Promise ? s.push(new i((g, y) => {
|
|
253
|
+
e[c].then(g).catch(y);
|
|
254
|
+
}, t)) : s.push(i.resolve(e[c]));
|
|
255
|
+
return { f: o, resolve: n, reject: r, array: s };
|
|
256
|
+
}, C(x, v);
|
|
257
|
+
export {
|
|
258
|
+
L as FUTURABLE_STATUS,
|
|
259
|
+
x as Futurable
|
|
260
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export type FuturableOnfulfilled<T, TResult2 = never> = ((value: any) => T | TResult2 | FuturableLike<T | TResult2> | PromiseLike<T | TResult2>) | undefined | null;
|
|
2
|
+
export type FuturableOnrejected<TResult = never> = ((reason: any) => TResult | FuturableLike<TResult> | PromiseLike<TResult>) | undefined | null;
|
|
3
|
+
export type FuturableResolveType<T> = T | FuturableLike<T> | PromiseLike<T>;
|
|
4
|
+
export interface FuturableResolve<T> {
|
|
5
|
+
(value?: FuturableResolveType<T>): void;
|
|
6
|
+
}
|
|
7
|
+
export interface FuturableReject {
|
|
8
|
+
(reason?: any): void;
|
|
9
|
+
}
|
|
10
|
+
export interface FuturableUtils<T> {
|
|
11
|
+
signal: AbortSignal;
|
|
12
|
+
cancel: () => void;
|
|
13
|
+
onCancel: (cb: () => void) => void;
|
|
14
|
+
delay: (cb: () => any, timer: number) => Futurable<T>;
|
|
15
|
+
sleep: (timer: number) => Futurable<T>;
|
|
16
|
+
fetch: (url: string, opts?: RequestInit) => Futurable<T>;
|
|
17
|
+
futurizable: (promise: Promise<T>) => Futurable<T>;
|
|
18
|
+
}
|
|
19
|
+
export type FuturableExecutor<T> = (resolve: FuturableResolve<T>, reject: FuturableReject, utils: FuturableUtils<T>) => void;
|
|
20
|
+
export type FuturableIterable = Futurable<any> | Promise<any> | any;
|
|
21
|
+
export interface FuturableLike<T> {
|
|
22
|
+
then<TResult1 = T, TResult2 = never>(onfulfilled?: FuturableOnfulfilled<TResult1, TResult2>, onrejected?: FuturableOnrejected<TResult2>): FuturableLike<TResult1 | TResult2>;
|
|
23
|
+
}
|
|
24
|
+
export declare enum FUTURABLE_STATUS {
|
|
25
|
+
PENDING = "pending",
|
|
26
|
+
FULFILLED = "fulfilled",
|
|
27
|
+
REJECTED = "rejected"
|
|
28
|
+
}
|
|
29
|
+
export declare class Futurable<T> extends Promise<T> {
|
|
30
|
+
#private;
|
|
31
|
+
constructor(executor: FuturableExecutor<T>, signal?: AbortSignal);
|
|
32
|
+
static get [Symbol.species](): typeof Futurable;
|
|
33
|
+
get [Symbol.toStringTag](): string;
|
|
34
|
+
get signal(): AbortSignal;
|
|
35
|
+
then<TResult1 = T, TResult2 = never>(onFulfilled: FuturableOnfulfilled<TResult1, TResult2>, onRejected?: FuturableOnrejected<TResult2>): Futurable<TResult1 | TResult2>;
|
|
36
|
+
catch<TResult = never>(onRejected: FuturableOnrejected<TResult>): Futurable<T | TResult>;
|
|
37
|
+
finally(onFinally: () => void): Futurable<void>;
|
|
38
|
+
cancel(): void;
|
|
39
|
+
delay<TResult1 = T, TResult2 = never>(cb: (val?: TResult1) => any, timer: number): Futurable<TResult1 | TResult2>;
|
|
40
|
+
sleep<TResult1 = T, TResult2 = never>(timer: number): Futurable<TResult1 | TResult2>;
|
|
41
|
+
fetch<TResult1 = T, TResult2 = never>(url: string | ((val?: TResult1) => string), opts?: object | RequestInit | ((val?: TResult1) => RequestInit)): Futurable<TResult1 | TResult2>;
|
|
42
|
+
onCancel<TResult1 = T, TResult2 = never>(cb: () => void): Futurable<TResult1 | TResult2>;
|
|
43
|
+
futurizable<TResult1 = T, TResult2 = never>(promise: Promise<TResult1> | ((val?: TResult1) => Promise<TResult1>)): Futurable<TResult1 | TResult2>;
|
|
44
|
+
static resolve(value?: any, signal?: AbortSignal): Futurable<any>;
|
|
45
|
+
static reject(reason?: any, signal?: AbortSignal): Futurable<any>;
|
|
46
|
+
static onCancel({ cb, signal }: {
|
|
47
|
+
cb: () => void;
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
}): Futurable<any>;
|
|
50
|
+
static delay({ cb, timer, signal }: {
|
|
51
|
+
cb: () => any;
|
|
52
|
+
timer: number;
|
|
53
|
+
signal?: AbortSignal;
|
|
54
|
+
}): Futurable<any>;
|
|
55
|
+
static sleep({ timer, signal }: {
|
|
56
|
+
timer: number;
|
|
57
|
+
signal?: AbortSignal;
|
|
58
|
+
}): Futurable<any>;
|
|
59
|
+
static fetch(url: string, opts?: RequestInit): Futurable<any>;
|
|
60
|
+
static futurizable<TResult1 = any, TResult2 = never>({ promise, signal }: {
|
|
61
|
+
promise: Promise<TResult1>;
|
|
62
|
+
signal?: AbortSignal;
|
|
63
|
+
}): Futurable<TResult1 | TResult2>;
|
|
64
|
+
static all(iterables: FuturableIterable[], signal?: AbortSignal): Futurable<any>;
|
|
65
|
+
static allSettled(iterables: FuturableIterable[], signal?: AbortSignal): Futurable<any>;
|
|
66
|
+
static race(iterables: FuturableIterable[], signal?: AbortSignal): Futurable<any>;
|
|
67
|
+
static any(iterables: FuturableIterable[], signal?: AbortSignal): Futurable<any>;
|
|
68
|
+
}
|
|
69
|
+
//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SAAS,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAuC3J,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,iBAAiB,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC,GAAG,CAAC;IAQhF,MAAM,CAAC,UAAU,CAAC,SAAS,EAAE,iBAAiB,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC,GAAG,CAAC;IAQvF,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,iBAAiB,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC,GAAG,CAAC;IAQjF,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,iBAAiB,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC,GAAG,CAAC;CAOhF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ndriadev/futurable",
|
|
3
|
+
"description": "Extension Javascript's Promise API with more functionalities",
|
|
4
|
+
"private": false,
|
|
5
|
+
"version": "1.0.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=16.13.2"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist/",
|
|
12
|
+
"scripts"
|
|
13
|
+
],
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/futurable.mjs",
|
|
18
|
+
"require": "./dist/futurable.cjs",
|
|
19
|
+
"default": "./dist/futurable.mjs",
|
|
20
|
+
"node": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"require": "./dist/futurable.cjs",
|
|
23
|
+
"import": "./dist/futurable.mjs",
|
|
24
|
+
"default": "./dist/futurable.cjs"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"main": "./src/index.ts",
|
|
29
|
+
"types": "./src/index.d.ts",
|
|
30
|
+
"scripts": {
|
|
31
|
+
"preinstall": "node ./scripts/preinstall.js --foreground-script",
|
|
32
|
+
"postinstall": "echo 'postinstall executed'",
|
|
33
|
+
"build": "tsc && vite build",
|
|
34
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
35
|
+
"lint": "eslint 'src/**/*.{js,jsx,ts,tsx}'",
|
|
36
|
+
"lint:fix": "eslint --fix 'src/**/*.{jsx,ts,tsx}'",
|
|
37
|
+
"prepare": "pnpm run build",
|
|
38
|
+
"prepublishOnly": "pnpm test && pnpm run lint"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@babel/preset-typescript": "^7.21.5",
|
|
42
|
+
"@jest/globals": "^29.5.0",
|
|
43
|
+
"@types/node": "^20.1.1",
|
|
44
|
+
"@typescript-eslint/eslint-plugin": "^5.59.5",
|
|
45
|
+
"@typescript-eslint/parser": "^5.59.5",
|
|
46
|
+
"eslint": "^8.40.0",
|
|
47
|
+
"eslint-config-prettier": "^8.8.0",
|
|
48
|
+
"eslint-plugin-import": "^2.27.5",
|
|
49
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
50
|
+
"jest": "^29.5.0",
|
|
51
|
+
"prettier": "^2.8.8",
|
|
52
|
+
"ts-jest": "^29.1.0",
|
|
53
|
+
"ts-node": "^10.9.1",
|
|
54
|
+
"typescript": "^5.0.2",
|
|
55
|
+
"vite": "^4.3.2",
|
|
56
|
+
"vite-plugin-dts": "^2.3.0",
|
|
57
|
+
"vite-plugin-linter": "^2.0.2",
|
|
58
|
+
"vite-tsconfig-paths": "^4.2.0"
|
|
59
|
+
},
|
|
60
|
+
"keywords": [
|
|
61
|
+
"promise",
|
|
62
|
+
"promises",
|
|
63
|
+
"promises-a",
|
|
64
|
+
"promises-aplus",
|
|
65
|
+
"async",
|
|
66
|
+
"await",
|
|
67
|
+
"deferred",
|
|
68
|
+
"deferreds",
|
|
69
|
+
"future",
|
|
70
|
+
"cancel",
|
|
71
|
+
"abort",
|
|
72
|
+
"delay",
|
|
73
|
+
"sleep",
|
|
74
|
+
"abortable",
|
|
75
|
+
"cancelable",
|
|
76
|
+
"futurable"
|
|
77
|
+
],
|
|
78
|
+
"repository": {
|
|
79
|
+
"type": "git",
|
|
80
|
+
"url": "git+https://github.com/nDriaDev/futurable"
|
|
81
|
+
},
|
|
82
|
+
"author": {
|
|
83
|
+
"name": "Andrea Cosentino",
|
|
84
|
+
"email": "andreacosentino.work@gmail.com",
|
|
85
|
+
"url": "https://github.com/nDriaDev/"
|
|
86
|
+
},
|
|
87
|
+
"readmeFilename": "README.md",
|
|
88
|
+
"bugs": {
|
|
89
|
+
"url": "https://github.com/nDriaDev/futurable/issues"
|
|
90
|
+
},
|
|
91
|
+
"homepage": "https://github.com/nDriaDev/futurable",
|
|
92
|
+
"license": "MIT"
|
|
93
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { exec } from 'child_process';
|
|
2
|
+
|
|
3
|
+
const result = process.versions;
|
|
4
|
+
|
|
5
|
+
if (result && result.node && parseInt(result.node) < 16) {
|
|
6
|
+
exec("npm install node-fetch", (err, stdout, sterr) => {
|
|
7
|
+
if (err) {
|
|
8
|
+
console.log("err", err.message);
|
|
9
|
+
process.exit(1);
|
|
10
|
+
} else if (sterr) {
|
|
11
|
+
console.log("stderr", sterr);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
console.log("stdout", stdout);
|
|
15
|
+
process.exit(0);
|
|
16
|
+
});
|
|
17
|
+
}
|