MCPcopy Index your code
hub / github.com/loopbackio/loopback-next / greetWithAsyncUser

Function greetWithAsyncUser

examples/context/src/sync-async.ts:49–65  ·  view source on GitHub ↗
(ctx: Context)

Source from the content-addressed store, hash-verified

47}
48
49async 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
67export async function main() {
68 const ctx = new Context('request');

Callers 1

mainFunction · 0.85

Calls 7

toDynamicValueMethod · 0.80
resolveMethod · 0.80
getSyncMethod · 0.80
getMethod · 0.65
logMethod · 0.65
helloMethod · 0.65
bindMethod · 0.45

Tested by

no test coverage detected