From 3c882a9d779ae9661c818fd3f58bbae0f2f7d24c Mon Sep 17 00:00:00 2001 From: meriadec Date: Wed, 14 Mar 2018 11:07:46 +0100 Subject: [PATCH] Prevent blocking main process when fetching cpu infos --- src/helpers/cpuUsage.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/helpers/cpuUsage.js b/src/helpers/cpuUsage.js index acf7b69a..e21d03ad 100644 --- a/src/helpers/cpuUsage.js +++ b/src/helpers/cpuUsage.js @@ -1,9 +1,9 @@ const TIMEOUT_CPU_USAGE = 5e3 -const cpuUsage = (startTime, startUsage) => { - const now = Date.now() +const wait = ms => new Promise(resolve => setTimeout(resolve, ms)) - while (Date.now() - now < 500); +const cpuUsage = async (startTime, startUsage) => { + await wait(500) const newStartTime = process.hrtime() const newStartUsage = process.cpuUsage() @@ -28,8 +28,8 @@ const cpuUsage = (startTime, startUsage) => { } export default callback => { - const initCpuUsage = (startTime, startUsage) => { - const { cpuPercent, newStartTime, newStartUsage } = cpuUsage(startTime, startUsage) + const initCpuUsage = async (startTime, startUsage) => { + const { cpuPercent, newStartTime, newStartUsage } = await cpuUsage(startTime, startUsage) callback(cpuPercent)