2022-08-08 23:06:36 +03:00
|
|
|
const {isMainThread, parentPort, workerData} = require('worker_threads');
|
2020-11-17 08:14:37 +03:00
|
|
|
const util = require('util');
|
|
|
|
const setTimeoutPromise = util.promisify(setTimeout);
|
|
|
|
|
|
|
|
const passTime = async (ms) => {
|
2020-12-02 11:26:15 +03:00
|
|
|
if (Number.isInteger(ms)) {
|
|
|
|
await setTimeoutPromise(ms);
|
|
|
|
} else {
|
|
|
|
await setTimeoutPromise(ms.ms);
|
|
|
|
}
|
2020-11-17 08:14:37 +03:00
|
|
|
};
|
|
|
|
|
2020-12-03 05:53:10 +03:00
|
|
|
if (isMainThread) {
|
|
|
|
module.exports = passTime;
|
|
|
|
} else {
|
|
|
|
(async () => {
|
|
|
|
await passTime(workerData.ms);
|
|
|
|
parentPort.postMessage('done');
|
|
|
|
// alternative way to signal "finished" work (not recommended)
|
|
|
|
// process.exit();
|
|
|
|
})();
|
|
|
|
}
|