( loader: () => Promise<InjectAsyncLoaderResult<T>>, options?: InjectAsyncOptions, )
| 56 | options?: InjectAsyncOptions, |
| 57 | ): () => Promise<T>; |
| 58 | export 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. |
no test coverage detected
searching dependent graphs…