1

I have an app which consists of services that are registered async.

First time before I use app I want app to register all services. I would want to somehow return a Promise and only then return specific service I need.

export function app() {
  if (instance) return instance;
  instance = express(feathers());

  ORM.default.then((data) => {
    setupServices();
    instance.configure(initServices);
    return instance; //this is needed before service usage
  });
  return instance;
}

export function service(name) {
  return app().service(name);
}

Other file:

import { service } from '../app';

const teamService = service('team');

At the moment teamService is null, as service register async and are not registered first time I import it.

Elminday
  • 53
  • 6

1 Answers1

1

You might want to return the promise directly

return ORM.default.then((data) => {
    setupServices();
    instance.configure(initServices);
    return instance; //this is needed before service usage
});

so that you can use app.then( ... )

Cristian S.
  • 818
  • 5
  • 13
  • But then I will have to resolve a promise with `service()` function too and where I use service I will have to use `.then` is that right? – Elminday May 22 '18 at 14:28
  • Actually I dont know if you really need the service(part) it just calls another function. If you use app directly you will be able to use `instance.service(name)` in the `then` part. – Cristian S. May 22 '18 at 14:44