murpjack
murpjack

Reputation: 15

how do I consume a future from a resolved fetch request?

I am using the coinbase dev API to access assets.
To access rates data requires an access key which the API sends back from a POST request.
I am using Flutures and a module called Fetch-Futures instead of just using the promise-based fetch API.

I haven't used Futures before. The response status is ok(200) but all options in the object I get back say resolved next to them (see screenshot).

What does it mean exactly that the type of each key inside the data object is resolved?

Does this mean I should be able to use the values already or do I need an additional step before I can return my access token?

I would like a nice way to access r.json[_value].access_token.

JS :

function exchangeCodeForAccessToken(tempCode) {
  const accessTokenURL = exchangeCode.url(tempCode);

  const getAccessToken = url => {
    console.log(fetchy);
    return fetchy(url, exchangeCode.options)
      .map(r => {
        console.log(2, r.json);
        return r;
      })
      .value(console.log);
  };

Upvotes: 0

Views: 330

Answers (1)

Avaq
Avaq

Reputation: 3031

From what I can tell, Fetch-Futures wraps all values in its return object in Futures:

https://github.com/theodesp/fetch-future/blob/10af776d2653eaf75aea580b9dd2a52ae9175abd/src/index.js#L42-L59

I'm not sure what the reason for this is*, but it means you will likely have to flatten the properties you need into the outer Future, like so:

return fetchy(url, exchangeCode.options)

// I'm using chain instead of map, because chain flattens the Future
// And since r.json is a Future, that works out:
.chain(r => r.json)

//Now I can map, and the value will be what was inside r.json:
.map(json => json.accessToken)

* If I were you, I would probably use something other than Fetch-Futures. It seems to be somewhat outdated and will likely not work with the latest version of Fluture, and uses a questionable API design with the wrapping of the output values.

Upvotes: 0

Related Questions