Skip to content

feat(core): Add a injectAsync helper function#68248

Open
JeanMeche wants to merge 1 commit intoangular:mainfrom
JeanMeche:inject-async
Open

feat(core): Add a injectAsync helper function#68248
JeanMeche wants to merge 1 commit intoangular:mainfrom
JeanMeche:inject-async

Conversation

@JeanMeche
Copy link
Copy Markdown
Member

@JeanMeche JeanMeche commented Apr 16, 2026

The commit introduces a new function to assist users who want to lazy load services and use the DI system to create them.

Example:

 * 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: () => { ... }});

@JeanMeche JeanMeche changed the title feat(core): Add injectAsync help function feat(core): Add a injectAsync helper function Apr 16, 2026
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: core Issues related to the framework runtime labels Apr 16, 2026
@ngbot ngbot bot added this to the Backlog milestone 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`');
});
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JeanMeche JeanMeche marked this pull request as ready for review April 16, 2026 16:03
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: () => { ... }});
 ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Issues related to the framework runtime detected: feature PR contains a feature commit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant