Sleep in JavaScript
via: https://stackoverflow.com/a/39914235/102401
Example with external function:
Code
return ;
}
;
await ;
;
// Sleep in loop
}
;
Example with sleep inside as one-liner
Code
}
;
Also:
Code
const { performance } = require('perf_hooks');
const delay = async (ms) => new Promise(resolve => setTimeout(resolve, ms));
const testSuite = async (func, arg) => {
const t0 = performance.now();
await func();
const t1 = performance.now();
console.log(t1 - t0);
}
const FuncToTest = async () => {
await delay(10000);
console.log("FuncToTest");
}
testSuite(FuncToTest)
- Must be in an async function