# Promise Hub

The promise hub is a solution for decoupling async tasks. Let's say, I would like to retrieve
the users from the database.
I could do it like this:

```javascript
// Generally we do something like this
const Users = require('users');
const users = new Users();
return users.find({}).exec();
```

But we can also do something like this:

```javascript
// Registering the promise (using Mongoose)
const promisehub = registry.get('promisehub');
promisehub.register('data:users', () => Users.find({}).exec());

// Retrieving users
const promisehub = registry.get('promisehub');
return promisehub.call('data:users');
```

# Why? What is this?

This way we can decouple data fetching, we don't have to be aware where is Model located, or how
to do the datafetching. We only need to know the name of the 'event'.

It was also important to do proper server-side rendering with React. On the server-side I needed to
know what data needs to be queried before I can render the view. I would have been an overhead, to
load them also through HTTP when we are on the server side.
This we only need to add the name of the registered promise, and the server will get all the data
before the view is rendered. [See more info on server-side rendering](Server-Side-Rendering.md)
