-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
38 lines (31 loc) · 1.08 KB
/
index.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
///////////////////////////////////////////////////////////
// wait-sync //
///////////////////////////////////////////////////////////
// a simple synchronous-yet-non-blocking "wait()" module
//
// 1. while you "wait()", async operations from the same
// execution block still work in the background (so
// non-blocking)
//
// 2. at the same time, the execution block will pseudo-run
// in a serial, linear manner
///////////////////////////////////////////////////////////
var deasync = require('deasync');
module.exports = (seconds=1) => {
if (typeof seconds !== 'number') {
throw new Error('waitSync :: invalid <seconds> argument' + seconds);
}
var isDone = false;
var start = new Date().getTime();
var msToWait = seconds * 1000;
var interval = setInterval(() => {
var now = new Date().getTime();
var delta = now - start;
if (delta >= msToWait){
clearInterval(interval);
isDone = true;
}
}, 25);
deasync.loopWhile(() => !isDone);
return;
};