-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path6-timeout-async.js
56 lines (48 loc) · 1.28 KB
/
6-timeout-async.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
// Wrapper will prevent call after timeout
const timeout = (fntime, cbtime, fn) => {
let timer = setTimeout(() => {
if (timer) console.log('Function timedout');
timer = null;
}, fntime);
return (...args) => {
if (timer) {
const callback = args[args.length - 1];
if (typeof callback === 'function') {
let cbTimer = setTimeout(() => {
if (cbTimer) console.log('Callback timedout');
cbTimer = null;
}, cbtime);
args[args.length - 1] = (...pars) => {
if (cbTimer) {
clearTimeout(cbTimer);
cbTimer = null;
return callback(...pars);
}
};
}
clearTimeout(timer);
timer = null;
return fn(...args);
}
};
};
// Usage
const fn = (par, callback) => {
console.log('Function called, par:', par);
setTimeout(() => callback(null, par), 150);
};
const fn100 = timeout(100, 150, fn);
const fn150 = timeout(150, 100, fn);
const fn200 = timeout(200, 250, fn);
setTimeout(() => {
fn100('first', (err, data) => {
console.log('Callback first', data);
});
fn150('second', (err, data) => {
console.log('Callback second', data);
});
fn200('third', (err, data) => {
console.log('Callback third', data);
});
}, 100);