MCPcopy Create free account
hub / github.com/angular/angular / injectAsync

Function injectAsync

packages/core/src/di/inject_async.ts:58–78  ·  view source on GitHub ↗
(
  loader: () => Promise<InjectAsyncLoaderResult<T>>,
  options?: InjectAsyncOptions,
)

Source from the content-addressed store, hash-verified

56 options?: InjectAsyncOptions,
57): () => Promise<T>;
58export function injectAsync<T>(
59 loader: () => Promise<InjectAsyncLoaderResult<T>>,
60 options?: InjectAsyncOptions,
61): () => Promise<T> {
62 if (ngDevMode) {
63 assertInInjectionContext(injectAsync);
64 }
65
66 const injector = inject(Injector);
67 const load = createLoader(loader);
68
69 if (options?.prefetch) {
70 options
71 .prefetch()
72 .then(load)
73 .catch(() => {});
74 }
75
76 // We can't use `inject` later on because of the async nature of the loader
77 return () => load().then((loadedToken) => injector.get(maybeUnwrapDefaultExport(loadedToken))!);
78}
79
80/**
81 * Creates the memoized loader function in its own lexical scope so that the

Callers 5

TestComponentClass · 0.90
PlaygroundComponentClass · 0.90
injectNodeRuntimeSandboxFunction · 0.90

Calls 9

assertInInjectionContextFunction · 0.90
injectFunction · 0.90
maybeUnwrapDefaultExportFunction · 0.90
loadFunction · 0.85
prefetchMethod · 0.80
createLoaderFunction · 0.70
thenMethod · 0.65
getMethod · 0.65
catchMethod · 0.45

Tested by

no test coverage detected