MCPcopy
hub / github.com/angular/angular / injectAsync

Function injectAsync

packages/core/src/di/inject_async.ts:58–85  ·  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
68 let loadedPromise: Promise<InjectAsyncLoaderResult<T>> | null = null;
69 const load = () => {
70 if (!loadedPromise) {
71 loadedPromise = loader();
72 }
73 return loadedPromise;
74 };
75
76 if (options?.prefetch) {
77 options
78 .prefetch()
79 .then(() => load())
80 .catch(() => {});
81 }
82
83 // We can't use `inject` later on because of the async nature of the loader
84 return () => load().then((loadedToken) => injector.get(maybeUnwrapDefaultExport(loadedToken))!);
85}
86
87/**
88 * Interface for `options` argument used within `injectAsync` call.

Callers 5

TestComponentClass · 0.90
PlaygroundComponentClass · 0.90
injectNodeRuntimeSandboxFunction · 0.90

Calls 7

assertInInjectionContextFunction · 0.90
injectFunction · 0.90
maybeUnwrapDefaultExportFunction · 0.90
loadFunction · 0.70
thenMethod · 0.65
getMethod · 0.65
catchMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…