We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 93f6f62 commit 4ea34aeCopy full SHA for 4ea34ae
1 file changed
JavaScript/b-expirable.js
@@ -0,0 +1,40 @@
1
+'use strict';
2
+
3
+const PROMISE_TIMEOUT = 1000;
4
5
+class Expirable extends Promise {
6
+ constructor(executor) {
7
+ super((resolve, reject) => {
8
+ executor(val => {
9
+ if (this.expired) {
10
+ reject(new Error('Expired'));
11
+ return;
12
+ }
13
+ clearTimeout(this.timer);
14
+ resolve(val);
15
+ }, reject);
16
+ });
17
+ this.expired = false;
18
+ this.timer = setTimeout(() => {
19
+ this.expired = true;
20
+ }, PROMISE_TIMEOUT);
21
22
+}
23
24
+// Usage
25
26
+new Expirable(resolve => {
27
+ setTimeout(resolve, 100);
28
+}).then(data => {
29
+ console.dir({ data });
30
+}).catch(error => {
31
+ console.dir({ error });
32
+});
33
34
35
+ setTimeout(resolve, 2000);
36
37
38
39
40
0 commit comments