Closed jeevan-jp closed 1 year ago
fixes #585
This is very wrong as it blocks the event loop. Run this
console.log(new Date());
const promise = new Promise(function (resolve) {
const now = new Date().getTime();
while (new Date().getTime() < now + 5000) {
/* Do nothing */
}
resolve(null);
});
console.log(new Date());
console.log('OK');
and notice how "OK" is printed after 5 seconds. Now run this
console.log(new Date());
const promise = new Promise(function (resolve) {
setTimeout(resolve, 5000);
});
console.log(new Date());
console.log('OK');
and see how "OK" is printed immediately.
Yeah, just went through this repo's delay function calls. You are right, blocking main thread would affect further processing by caller function.
I am still wondering whether there is a better implementation, since accuracy is something that's not guaranteed with setTimeout.
Seems there is none.
I am still wondering whether there is a better implementation, since accuracy is something that's not guaranteed with setTimeout.
It is a very small inaccuracy (few milliseconds at most) so I think it does not matter.
I'm closing this. Thank you anyway.
while loop has been used to improve delay function