feat(core): Add a injectAsync helper function#68248
Open
JeanMeche wants to merge 1 commit intoangular:mainfrom
Open
feat(core): Add a injectAsync helper function#68248JeanMeche wants to merge 1 commit intoangular:mainfrom
injectAsync helper function#68248JeanMeche wants to merge 1 commit intoangular:mainfrom
Conversation
injectAsync help functioninjectAsync helper function
JeanMeche
commented
Apr 16, 2026
Comment on lines
+143
to
+157
| it("should throw if the service wasn't provided in root", async () => { | ||
| class UnprovidedService {} | ||
|
|
||
| const injector = TestBed.inject(Injector); | ||
|
|
||
| let error!: Error; | ||
| try { | ||
| await injectAsync(() => Promise.resolve(UnprovidedService), {injector}); | ||
| } catch (e: any) { | ||
| error = e; | ||
| } | ||
|
|
||
| expect(error).toBeDefined(); | ||
| expect(error.message).toContain('No provider found for `UnprovidedService`'); | ||
| }); |
Member
Author
There was a problem hiding this comment.
We could also support this use case an create an intermediate injector.
The service would then be tied to the lifecycle of the current/option injector.
The commit introduces a new function to assist users who want to lazy load services and use the DI system to create them.
Example:
```ts
* import {onIdle} from '@angular/core';
*
* // Simple injection (likely most common scenario)
* await injectAsync(() => import('./my-huge-dependency'));
*
* // Injection which is triggered when a browser becomes idle
* // We provide `onIdle` helper as a common building block
* await injectAsync(() => import('./my-huge-dependency'), {trigger: onIdle});
*
* // Custom trigger condition
* injectAsync(() => import('./my-huge-dependency'), {trigger: () => { ... }});
```
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.
The commit introduces a new function to assist users who want to lazy load services and use the DI system to create them.
Example: