-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue-microtask.ts
34 lines (32 loc) · 1.1 KB
/
queue-microtask.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Please refer to the terms of the license agreement in the root of the project
*
* (c) 2024 Feedzai
*/
/**
* Queues a function to be called at the end of the current event loop.
*
* The queueMicrotask() method, which is exposed on the Window or Worker interface,
* queues a microtask to be executed at a safe time prior to control returning to
* the browser's event loop.
*
* The microtask is a short function which will run after the current task has completed
* its work and when there is no other code waiting to be run before control of
* the execution context is returned to the browser's event loop.
*
* This lets your code run without interfering with any other, potentially higher priority,
* code that is pending, but before the browser regains control over the execution context,
* potentially depending on work you need to complete.
*
* @example
*
* queueMicrotask(() => {
* element?.focus();
* });
*/
export function queueMicrotask(callback: VoidFunction): void {
if (window.queueMicrotask) {
return window.queueMicrotask(callback);
}
void Promise.resolve().then(callback);
}