Path: blob/master/node_modules/atomic-sleep/index.js
3978 views
'use strict'12/* global SharedArrayBuffer, Atomics */34if (typeof SharedArrayBuffer !== 'undefined' && typeof Atomics !== 'undefined') {5const nil = new Int32Array(new SharedArrayBuffer(4))67function sleep (ms) {8// also filters out NaN, non-number types, including empty strings, but allows bigints9const valid = ms > 0 && ms < Infinity10if (valid === false) {11if (typeof ms !== 'number' && typeof ms !== 'bigint') {12throw TypeError('sleep: ms must be a number')13}14throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')15}1617Atomics.wait(nil, 0, 0, Number(ms))18}19module.exports = sleep20} else {2122function sleep (ms) {23// also filters out NaN, non-number types, including empty strings, but allows bigints24const valid = ms > 0 && ms < Infinity25if (valid === false) {26if (typeof ms !== 'number' && typeof ms !== 'bigint') {27throw TypeError('sleep: ms must be a number')28}29throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')30}31const target = Date.now() + Number(ms)32while (target > Date.now()){}33}3435module.exports = sleep3637}383940