Skip to content

Enable RemoteData to take a promise #5

@jackfranklin

Description

@jackfranklin

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions