feat(loader): make preload() / load() await-able (Promise form)#1512
Merged
Conversation
preload(assets) now returns a Promise<void> that resolves once every asset has loaded (rejects on failure), so you can `await loader.preload` instead of nesting an onload callback. load(asset) called WITHOUT callbacks likewise returns a Promise for a one-off dynamic load. Fully back-compat: the callback forms, loader.onload, and the LOADER_PROGRESS / LOADER_ERROR / LOADER_COMPLETE events are unchanged, and load(asset, onload, onerror) still returns the resource count — the Promise form only kicks in when no callbacks are passed. Implementation: - preload uses load()'s promise form internally (dropped the per-asset new Promise(callback) wrapper); progress (onResourceLoaded) and error (onLoadingError + LOADER_ERROR) are layered via .then/.catch. - completeLoading no longer throws when no callback is set (await without a callback is now valid) and always emits LOADER_COMPLETE. - behavior notes: cached assets now count toward the progress total (loadCount/resourceCount stay balanced); LOADER_COMPLETE fires even with no callback. Tests: callback form (preload cb, load onload, load onerror, count) and promise form (await preload incl. no-callback, await load, both reject paths). Full suite green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
loader.preload()/loader.load()are nowawait-ableSmall, additive QoL — modern callers expect to
awaitthe loader instead of nesting anonloadcallback.API
await loader.preload(assets)— returns aPromise<void>that resolves once every asset has loaded, and rejects on failure.await loader.load(asset)— the no-callback form returns a Promise for a one-off dynamic load;load(asset, onload, onerror)still returns the resource count.Fully back-compat
The callback forms,
loader.onload, and theLOADER_PROGRESS/LOADER_ERROR/LOADER_COMPLETEevents are unchanged. The Promise form only activates when no callbacks are passed, so every existingpreload(assets, cb)/load(asset, onload, onerror)call behaves exactly as before (same code path, same callback invocation, same count return).Implementation
preloadwas alreadyPromise.all-based internally — it just didn't return the promise. It now does, and it usesload()'s new promise form per-asset (dropping the per-assetnew Promise(callback)wrapper); progress (onResourceLoaded) and error (onLoadingError+LOADER_ERROR) are layered via.then/.catch.completeLoadingno longer throws when no callback is set (so a bareawait preload([...])resolves on success instead of rejecting) and always emitsLOADER_COMPLETE.onLoadingErrorthrows (no retry — retry isloader.reload()), which previously escaped the parser's async error callback; an awaitedpreload/loadnow rejects cleanly.Behavior notes (minor, intentional)
loadCount/resourceCountstay balanced; fresh loads unchanged).LOADER_COMPLETEnow fires even when no callback is provided.Tests
New
callback form (legacy)andpromise form (await)sub-describes covering both forms side-by-side (preload + load success/error/count, await-with-and-without-callback, both reject paths). Full suite green (4286 / 0 failed);pnpm build(eslint + biome + types) clean.CHANGELOG + the
resourceswiki page updated.🤖 Generated with Claude Code