Skip to content

feat(loader): make preload() / load() await-able (Promise form)#1512

Merged
obiot merged 1 commit into
masterfrom
feat/loader-await
Jun 22, 2026
Merged

feat(loader): make preload() / load() await-able (Promise form)#1512
obiot merged 1 commit into
masterfrom
feat/loader-await

Conversation

@obiot

@obiot obiot commented Jun 22, 2026

Copy link
Copy Markdown
Member

loader.preload() / loader.load() are now await-able

Small, additive QoL — modern callers expect to await the loader instead of nesting an onload callback.

API

  • await loader.preload(assets) — returns a Promise<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 the LOADER_PROGRESS / LOADER_ERROR / LOADER_COMPLETE events are unchanged. The Promise form only activates when no callbacks are passed, so every existing preload(assets, cb) / load(asset, onload, onerror) call behaves exactly as before (same code path, same callback invocation, same count return).

Implementation

  • preload was already Promise.all-based internally — it just didn't return the promise. It now does, and it uses load()'s new promise form per-asset (dropping 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 (so a bare await preload([...]) resolves on success instead of rejecting) and always emits LOADER_COMPLETE.
  • Surfaced + fixed a latent issue the new tests caught: onLoadingError throws (no retry — retry is loader.reload()), which previously escaped the parser's async error callback; an awaited preload/load now rejects cleanly.

Behavior notes (minor, intentional)

  • Cached assets now count toward the progress total (loadCount/resourceCount stay balanced; fresh loads unchanged).
  • LOADER_COMPLETE now fires even when no callback is provided.

Tests

New callback form (legacy) and promise 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 resources wiki page updated.

🤖 Generated with Claude Code

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>
@obiot obiot merged commit 4c2e08f into master Jun 22, 2026
6 checks passed
@obiot obiot deleted the feat/loader-await branch June 22, 2026 07:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant