5

I know a similar question was asked before but it doesn't quite answer what I'm trying to understand.

I'm reading the mobx-state-router tutorial and it has this piece of code:

{
    name: 'department',
    pattern: '/departments/:id',
    onEnter: (fromState, toState, routerStore) => {
        const { rootStore: { itemStore } } = routerStore;
        itemStore.loadDepartmentItems(toState.params.id);
        return Promise.resolve();
    }
},

I don't understand what this Promise.resolve() relates to? Which promise is it? How/when is it resolved?

Avi
  • 21,182
  • 26
  • 82
  • 121

1 Answers1

5

Which promise is it?

Promise.resolve creates a new promise.

How/when is it resolved?

Immediately, that's what Promise.resolve is for.

onEnter is apparently expected to return a promise. In this particular onEnter, it doesn't have any asynchronous work to do, so it returns a pre-resolved promise. Normally, you specify the value the promise should be resolved with (Promise.resolve(42)), but if you don't, the usual JavaScript semantics apply and the value that is used to resolve the promise is the value undefined.

See Promise.resolve in the spec and on MDN.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875