-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathwait_async.js
More file actions
36 lines (27 loc) · 709 Bytes
/
wait_async.js
File metadata and controls
36 lines (27 loc) · 709 Bytes
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
'use strict';
const {
PromisePrototypeThen
} = primordials;
const timers = require('timers');
const keepAliveInterval = 2 ** 31 - 1;
let pendingWaiters = 0;
let keepAliveHandle = null;
function maybeStopKeepAlive() {
if (--pendingWaiters === 0) {
timers.clearInterval(keepAliveHandle);
keepAliveHandle = null;
}
}
function trackWaitAsyncResult(result) {
if (!result.async) {
return result;
}
if (++pendingWaiters === 1) {
keepAliveHandle = timers.setInterval(() => {}, keepAliveInterval);
}
PromisePrototypeThen(result.value, maybeStopKeepAlive, maybeStopKeepAlive);
return result;
}
module.exports = {
trackWaitAsyncResult,
};