-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmodule.js
More file actions
39 lines (37 loc) · 884 Bytes
/
module.js
File metadata and controls
39 lines (37 loc) · 884 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
37
38
39
/* Sleep function for Node.
*
* @package system-sleep
* @version 1.3.7
* @author Jochem Stoel (http://jochemstoel.github.io)
* @license don't involve me
*/
const sleep = interval => new Promise((deliver, renege) => {
if(typeof interval != 'number' || interval < 1) deliver(
new Date().getTime()
)
try {
setTimeout(() => {
deliver(
new Date().getTime()
)
}, interval)
} catch (exception) {
renege(
exception.message
)
}
})
module.exports = interval => {
if(typeof interval != 'number' || interval < 1)
return new Date().getTime()
try {
return require('deasync-promise')(
sleep(interval)
)
} catch (notAsync) { /* https://github.com/jochemstoel/nodejs-system-sleep/issues/4 */
require('child_process').execSync(
`"${process.execPath}"` + " -e \"setTimeout(function () { return true; }, " + interval + ");\""
)
return null
}
}