Gets a PromiseLike that resolves inside window.requestAnimationFrame callback.
a token used to cancel the timeout with window.cancelAnimationFrame.
a PromiseLike that resolves inside window.requestAnimationFrame callback.
Resolved value contains RAF context.
Gets a PromiseLike that resolves inside window.requestIdleCallback callback.
It basically executes the callback when the JavaScript event loop is idle, or the specified timeout has reached.
options passed into requestIdleCallback function.
a token used to cancel the timeout with window.cancelIdleCallback.
a PromiseLike that resolves inside window.requestIdleCallback callback.
Resolved value contains requestIdleCallback context.
Gets a PromiseLike that resolves inside window.setTimeout callback.
the delay in milliseconds before the PromiseLike resolves.
a token used to cancel the timeout with window.clearTimeout.
a PromiseLike that resolves inside window.setTimeout callback.
Resolved value is the timeout ID returned from window.setTimeout.
Generated using TypeDoc
Contains adapter functions that allows you to switch into another context when you subscribe the returned
PromiseLikes. For example, in async functions, if you want to execute part of your logic inside an animation frame, you may now use a simple loop inside the asynchronous function.import { ICancellationToken, requestAnimationFrameAsync } from "tasklike-promise-library"; async function playAnimation(cancellationToken?: ICancellationToken): Promise<void> { cancellationToken && cancellationToken.throwIfCancellationRequested(); let prevTime = performance.now(); let currentWidth = 0; const panel = document.querySelector("div.panel"); while (!cancellationToken || !cancellationToken.isCancellationRequested) { const animationFrame = await requestAnimationFrameAsync(cancellationToken); // From now on, we are inside RAF callback. // `animationFrame` contains the information passed from RAF callback, // basically, the start time of the animation frame. // Make some animation here. const frameDuration = animationFrame.time - prevTime; // Let the width of .panel increase by 10 pixel/sec. currentWidth += frameDuration * 10 * frameDuration / 1000; panel.styles.with = Math.round(currentWidth) + "px"; prevTime = animationFrame.time; // End of RAF callback. // (More precisely, the callback stop at the next `await requestAnimationFrameAsync` expression.) // The next `await requestAnimationFrameAsync` expression will let us enter another RAF callback. // To exit the current RAF callback and enter another arbitrary asynchronous callback in the same async function, // Use `yielded()` or `delay(0)` from `"common"` module. } }