In this lesson, we’re exploring how asynchronous functions can be seamlessly called within a promise chain — just like any other function that returns a promise.
In this example, we await
the fetch
call and then we await
the json parsing. Is it the same thing to await
a fetch(url).json()
?
No, that's not the same.
fetch()
returns a Promise
, which has a then()
method, but no json()
method. Therefore, you can't do await fetch(url).json()
. You could, however, do await fetch(url).then(res => res.json())
.
very nice tutorial, thx for that!
one question: at 00:42 you are demonstrating the function return value by hovering over the function execution. which text editor/plugin is enabling this?
No, that's not the same.
fetch()
returns a Promise
, which has a then()
method, but no json()
method. Therefore, you can't do await fetch(url).json()
. You could, however, do await fetch(url).then(res => res.json())
.
YOU summarized in a 1 line reply what so many experts and CS educators failed! BLESS YOU MAN 👏🏻
No, that's not the same.
fetch()
returns a Promise
, which has a then()
method, but no json()
method. Therefore, you can't do await fetch(url).json()
. You could, however, do await fetch(url).then(res => res.json())
.
YOU summarized in a 1 line reply what so many experts and CS educators failed! BLESS YOU MAN 👏🏻
ESlint rules prescribe against using return await in an async function Eslint rules documentation . here https://eslint.org/docs/rules/no-return-await Since the return value of an async function is always wrapped in Promise.resolve, return await doesn’t actually do anything except add extra time before the overarching Promise resolves or rejects. The only valid exception is if return await is used in a try/catch statement to catch errors from another Promise-based function.
having return response.json();
without await should be good here, you are returning a promise as normal promsie and should flatten one promise for you.