NetworkError on aborting PixiJS Loader fetch
0 reputation · 17 Aug 2025, 01:31 UTC
0 reputation · 17 Aug 2025, 01:31 UTC
Gracefully cancel or timeout an ongoing resource load in PixiJS when the application needs to abort a request or the request takes too long.
PixiJS Loader uses fetch or XMLHttpRequest internally. The Loader does not expose a timeout or abort interface; its destroy method only frees GPU memory and does not terminate the network request. When the underlying fetch/XHR is aborted manually, the Loader emits a generic NetworkError and the promise returned by load() rejects with that error, but this behavior is undocumented and inconsistent across browsers.
There is no documented path for cancelling an in‑progress load or setting a timeout. Developers must rely on manual aborts of the underlying request, which may lead to unpredictable errors.
31775 reputation · 17 Aug 2025, 10:34 UTC
PixiJS 6.x does not provide a native abort() or timeout method within the Loader class. To cancel an in-progress load or enforce a timeout, you must wrap the loader.load() promise in a Promise.race() and manually trigger the underlying browser request cancellation.
Because the Loader abstracts the transport layer, you cannot call abort() directly on the Loader instance. Instead, implement a timeout wrapper that rejects the operation and handles the resulting NetworkError.
const timeout = (ms) => new Promise((_, reject) =>
setTimeout(() => reject(new Error('Timeout')), ms)
);
try {
// Race the loader against a timer
await Promise.race([
loader.load("assets.json"),
timeout(5000)
]);
} catch (err) {
if (err.message === 'Timeout') {
console.warn('Load timed out. Note: The underlying fetch may still continue until the browser terminates it.');
} else {
// PixiJS wraps aborted fetches/XHR as 'NetworkError'
console.error('Resource load failed:', err);
}
}
AbortController signals.AbortController), the browser throws an AbortError. PixiJS catches this and re-throws it as a generic NetworkError.load() will reject upon abortion, but it does not provide a distinct error type to differentiate a manual abort from a server-side failure.This behavior assumes a PixiJS 6.x environment using the Fetch API transport. In older versions relying on XMLHttpRequest, the error surfacing may differ slightly depending on the browser's implementation of XHR aborts. There is currently no official roadmap indicating a high-level abort() API for the Loader in upcoming releases.
Promise.race pattern shown above.(canceled)..catch() block receives an error containing the string "NetworkError".Diagnostic Detail Needed: Are you using a custom resource loader or the default PixiJS asset manifest system? Custom loaders may allow for direct AbortController integration.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.