()
| 380 | // implementation taken from playwright: https://github.com/microsoft/playwright/blob/59d0f8728d4809b39785d68d7a146f06f0dbe2e6/src/helper.ts#L233 |
| 381 | // See https://joel.tools/microtasks/ |
| 382 | function makeWaitForNextTask() { |
| 383 | if (parseInt(process.versions.node, 10) >= 11) return setImmediate; |
| 384 | |
| 385 | // Unlike Node 11, Node 10 and less have a bug with Task and MicroTask execution order: |
| 386 | // - https://github.com/nodejs/node/issues/22257 |
| 387 | // |
| 388 | // So we can't simply run setImmediate to dispatch code in a following task. |
| 389 | // However, we can run setImmediate from-inside setImmediate to make sure we're getting |
| 390 | // in the following task. |
| 391 | |
| 392 | let spinning = false; |
| 393 | const callbacks: (() => void)[] = []; |
| 394 | const loop = () => { |
| 395 | const callback = callbacks.shift(); |
| 396 | if (!callback) { |
| 397 | spinning = false; |
| 398 | return; |
| 399 | } |
| 400 | setImmediate(loop); |
| 401 | // Make sure to call callback() as the last thing since it's |
| 402 | // untrusted code that might throw. |
| 403 | callback(); |
| 404 | }; |
| 405 | |
| 406 | return (callback: () => void) => { |
| 407 | callbacks.push(callback); |
| 408 | if (!spinning) { |
| 409 | spinning = true; |
| 410 | setImmediate(loop); |
| 411 | } |
| 412 | }; |
| 413 | } |
no test coverage detected