-
Notifications
You must be signed in to change notification settings - Fork 723
Description
I want to inject a promise, not the value the promise resolves to.
Expected Behavior
const keyN = Symbol();
const keyP = Symbol();
container.bind(keyN).toDynamicValue(() => 42).inSingletonScope();
container.bind(keyP).toDynamicValue(() => functionThatReturnsAPromise()).inSingletonScope();
const myNumber = container.get(keyN); // Works.
const myPromise = container.get(keyP); // Throws.
... // some more stuff
const value = await Promise.all(myPromise, ...otherPromises);
Current Behavior
_getButThrowIfAsync
throws an error.
You are attempting to construct '" + key + "' in a synchronous way\n but it has asynchronous dependencies.
But, I don't want the Value the Promise returns.. I want THE PROMISE itself. The promise IS the value.
Possible Solution
a container.get()
that bypasses _getButThrowIfAsync
and gets the exact thing I want.
Steps to Reproduce (for bugs)
I have optionally commented out the types and a bunch of other boilerplate, but you get the idea.
const { Container } = require("inversify");
/* type DIKey<T> = {}; ideally it should extend Symbol, but that had problems in current TS version */
class DIBinder {
constructor(container) {
this.container = container;
}
bindConstant/*<T>*/(key/*: DIKey<T>*/, value/*: T*/) {
this.container.bind(key).toConstantValue(value);
}
bindDynamic/*<T>*/(key/*: DIKey<T>*/, func/*: () => T*/) {
this.container.bind(key).toDynamicValue(() => func()).inSingletonScope();
}
}
class DIResolver {
constructor(container) {
this.container = container;
}
get/*<T>*/(key/*: DIKey<T>*/)/*: T */ {
// (some of our own logic)
return this.container.get(key);
}
}
const container = new Container();
const diBinder = new DIBinder(container);
const diResolver = new DIResolver(container);
const keyN = Symbol(); /* as DIKey<number>; */
const keyP = Symbol(); /* as DIKey<Promise<number>>; */
diBinder.bindDynamic(keyN, () => Math.random() * 42); // Key knows function MUST supply a number.
diBinder.bindDynamic(keyP, async () => Math.random() * 42); // Key knows function MUST supply a Promise<number>.
const myNumber = diResolver.get(keyN); // Key knows get() returns a number;
const myPromise = diResolver.get(keyP); // Key knows get() SHOULD returns return Promise of number;
Context
We are midway through the process of changing over everything to 'real' inversify with actual injection from constructors and stuff,
but it's a big codebase and we are nowhere near finished, but we wanted to start getting things setup and ready, and doing our best to use it with vue3 composibles.
but we found that this "throwing if it's a promise" is slowing us down in our interim because we need to keep switching between get and getAsync when it really isn't needed at this point.
We kind of need a container.get_Even_If_Its_A_Promise_I_Dont_Care(key)
function. So everything can go through a single call on the container.
Your Environment
- Version used: ^6.0.1
Stack trace
Metadata
Metadata
Assignees
Labels
Type
Projects
Status