Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable RemoteData to take a promise #5

Open
jackfranklin opened this issue Mar 1, 2017 · 5 comments
Open

Enable RemoteData to take a promise #5

jackfranklin opened this issue Mar 1, 2017 · 5 comments

Comments

@jackfranklin
Copy link
Owner

Under the hood right now Remote Data uses fetch directly, but in many real world apps (like the one I'd like to use this in!) this is a problem, because we use our own API client that has special headers, etc, etc set up.

Rather than pass all this into RemoteData it would be much better if along with calling fetch you could also give it a promise that should resolve or error accordingly.

Doing this would:

  • give you a RemoteData that's immediately in the PENDING state (and not fire an onChange for the NOT_ASKED => PENDING transition
  • If the promise resolves, we go to SUCCESS and provide whatever the promise resolved with as the data
  • If it rejects, we go to FAILURE and provide the error data.

I think this should be a new method on RemoteData ? Could be something like:

// 1
import RemoteData from 'remote-data-js'

const myPromise = myHttpClient.get('/foo')
const customRemote = RemoteData.fromPromise(myPromise, {
  onChange: newRemote => {
     // either succeeded with the data or failed
  }
})
customRemote.isPending() => true

// OR:

const customRemote = RemoteData.fromPromise(myPromise).onChange(fn)
@jackfranklin
Copy link
Owner Author

@isaacseymour I feel like you might have thoughts :)

@jackfranklin
Copy link
Owner Author

jackfranklin commented Mar 1, 2017

Also if we did this it might be nice to hide the actual class under the hood and turn the top level API into:

import { withPromise, withFetch } from 'remote-data-js'

const remoteData  = withPromise(promise, { onChange })
const remoteData = withFetch(url, {onChange, fetchOptions, ...})

Would prevent more breaking changes in the future.

@joshhornby
Copy link

Hi @jackfranklin Any updates with this?

@jackfranklin
Copy link
Owner Author

No - not yet! PRs' welcome :)

@jackfranklin
Copy link
Owner Author

@joshhornby

You can now do import { withPromise } from 'remote-data-js'

Not yet got the fromFetch API (see README for current details).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants