goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 323 forks source link

Return Promise on calling source methods #618

Closed vahnag closed 1 month ago

vahnag commented 8 years ago

What do you think about returning Promise which resolves with the value when a source method is called?

For example:

const TestSource = {
  goFetchIt: {
    remote() {
      return Promise.resolve('value from source');
    },
    success: actions.test,
    error: actions.test,
  }
}

And then:

TestStore.goFetchIt().then(value => {
  value === 'value from source' // true
});

I'm working on an app and just success isn't enough for me, I'd like to be able to know when the async is finished in UI without storing everything in alt store, so I had to hack something with passing callbacks as arguments.

I noticed that alt already returns promise when remote is called (without the value) and just the value if it takes it from local. So why not use a promise in both cases and resolve with actual value? Am I missing anything?

mull commented 8 years ago

I always return a resolving promise from my data sources. Promises from a data source should not be resolving with a value though, values should be kept in the store. A bit inconvenient, but keeping the truth in the store is the way to go with flux/alt (IMO.)

vahnag commented 8 years ago

I ended up using Promise.resolve(TestStore.goFetchIt()) and getting the new value from store. Not very convenient, but works.

I still think it will be great to have a promise resolving with value though, that will make it much simpler.