async-didi

Async Dependency Injection for JavaScript

Usage no npm install needed!

<script type="module">
  import asyncDidi from 'https://cdn.skypack.dev/async-didi';
</script>

README

async-didi

Build Status

An async version of didi, the tiny dependency injection container for JavaScript.

Example

import {
  AsyncInjector
} from 'async-didi';

function Car(engine) {
  this.start = function() {
    engine.start();
  };
}

async function createEngine(power) {
  return {
    start: function() {
      console.log('Starting engine with ' + power + 'hp');
    }
  };
}

const injector = new AsyncInjector([
  {
    'car': ['type', Car],
    'engine': ['factory', createEngine],
    'power': ['value', 1184]
  }
]);

await injector.invoke(async function(car) {
  await car.start();
});

For more examples, check out the tests.

Comparison to didi

  • Same core features
  • Exposes an AsyncInjector
  • Injector API functions get, invoke and instantiate are async
  • Factory functions may be async
  • No support for child injectors and scopes

License

MIT