Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/atomic-sleep/index.js
1126 views
1
'use strict'
2
3
/* global SharedArrayBuffer, Atomics */
4
5
if (typeof SharedArrayBuffer !== 'undefined' && typeof Atomics !== 'undefined') {
6
const nil = new Int32Array(new SharedArrayBuffer(4))
7
8
function sleep (ms) {
9
// also filters out NaN, non-number types, including empty strings, but allows bigints
10
const valid = ms > 0 && ms < Infinity
11
if (valid === false) {
12
if (typeof ms !== 'number' && typeof ms !== 'bigint') {
13
throw TypeError('sleep: ms must be a number')
14
}
15
throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
16
}
17
18
Atomics.wait(nil, 0, 0, Number(ms))
19
}
20
module.exports = sleep
21
} else {
22
23
function sleep (ms) {
24
// also filters out NaN, non-number types, including empty strings, but allows bigints
25
const valid = ms > 0 && ms < Infinity
26
if (valid === false) {
27
if (typeof ms !== 'number' && typeof ms !== 'bigint') {
28
throw TypeError('sleep: ms must be a number')
29
}
30
throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
31
}
32
const target = Date.now() + Number(ms)
33
while (target > Date.now()){}
34
}
35
36
module.exports = sleep
37
38
}
39
40