[Vuejs]-Weird error when using javascript async/await syntax

0👍

const result = await res.json().results[0];

You are directly accessing results data ( which might not be assured/yielded yet) instead of waiting for the res.json() to finish processing

EDIT

It is not assured that res.json() will yield any data yet, or there will be any proper response at all. So accessing data is not reasonable yet

await is getting called on results[0]
its not getting called on res.json();

To simplify, what you are actually doing is this

results = res.json();
const result = await results[0];

So there are two logical errors in this

0👍

await expects a promise in return. json() returns such a promise, but json().results is just an object (which is even undefined at start). Thats why await doesn’t work on that structure.

0👍

Check if res.json().results is not an empty array;

const result = (await res.json()).results.[0];

Leave a comment