0

I am trying to fetch some data and then access the values in the response but I keep running into Promise which I can't seem to get out of to access my values.

Here is my request:

var boom = fetch(uri, options).then(response => response.text());

The response is:

Promise {<resolved>: "{"word":"bump","results":[{"definition":"come upon…,"pronunciation":{"all":"bəmp"},"frequency":4.02}"} __proto__: Promise [[PromiseStatus]]: "resolved" [[PromiseValue]]: "{"word":"bump","results":[{"definition":"come upon, as if by accident; meet with"}]};

I am not sure how I would access the value associated with "bump" or "definition". None of the normal ways I would access these values ([] or .) seem to be working.

I am assuming this a JSON object that I can access the values from but I might be wrong; whenever I try typeOf I just get object.

Alex
  • 441
  • 2
  • 5
  • 12
  • The problem is, that `response.text()` returns a promise, either use `then` on it, or await it – Luca Kiebel Sep 27 '18 at 20:03
  • It is still unclear how to access the values even if I do `var boom = fetch(uri, options).then(response => response.text().then());` – Alex Sep 27 '18 at 20:33
  • No, read the dupe target. `fetch(uri, options).then(response => response.text()).then(boom));` or `var boom = await fetch(uri, options).then(response => response.text())` – Luca Kiebel Sep 27 '18 at 20:36
  • Okay, I figured it out. `var boom = fetch(uri, options).then(response => response.json().then(function(data) {console.log(data.word);}));` – Alex Sep 27 '18 at 20:57
  • Yeah, though you should omit the `var boom =` then, it's just gonna be `undefined` – Luca Kiebel Sep 27 '18 at 21:09

0 Answers0