| 29 | const GREETER = BindingKey.create<Greeter>('greeter'); |
| 30 | |
| 31 | async function greetWithSyncUser(ctx: Context) { |
| 32 | // Set the current user to `John` (a constant value) |
| 33 | // As a result, CURRENT_USER can be resolved synchronously |
| 34 | ctx.bind(CURRENT_USER).to('John (sync)'); |
| 35 | |
| 36 | // Greeter has a dependency on current user which can be resolved |
| 37 | // synchronously. This allows GREETER to be resolved either synchronously or |
| 38 | // asynchronously. |
| 39 | // Get an instance of Greeter synchronously |
| 40 | let greeter = ctx.getSync(GREETER); |
| 41 | console.log('%s', greeter.hello()); |
| 42 | |
| 43 | // Get an instance of Greeter asynchronously |
| 44 | greeter = await ctx.get(GREETER); |
| 45 | console.log('%s', greeter.hello()); |
| 46 | return greeter; |
| 47 | } |
| 48 | |
| 49 | async function greetWithAsyncUser(ctx: Context) { |
| 50 | // Now set the current user to an async factory |