| 47 | } |
| 48 | |
| 49 | async function greetWithAsyncUser(ctx: Context) { |
| 50 | // Now set the current user to an async factory |
| 51 | // As a result, CURRENT_USER can only be resolved asynchronously |
| 52 | |
| 53 | ctx.bind(CURRENT_USER).toDynamicValue(() => Promise.resolve('Jane (async)')); |
| 54 | // Get an instance of Greeter asynchronously |
| 55 | let greeter = await ctx.get(GREETER); |
| 56 | console.log('%s', greeter.hello()); |
| 57 | try { |
| 58 | // Get an instance of Greeter synchronously - THIS WILL FAIL |
| 59 | greeter = ctx.getSync(GREETER); |
| 60 | console.log(greeter.hello()); |
| 61 | } catch (err) { |
| 62 | // Error: Cannot get greeter synchronously: the value is a promise |
| 63 | console.log('Expect to fail with error: %s', err.message); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | export async function main() { |
| 68 | const ctx = new Context('request'); |