How to use fetch with async/await?

前端 未结 1 1456
我寻月下人不归
我寻月下人不归 2021-01-05 05:49

I start by saying that I am not 100% sure this is the problem, I mean using await and async.

This is the scenario:

I run this when I first load the page, and

相关标签:
1条回答
  • 2021-01-05 06:12

    Referring to this article should take care of your issue. See the snippet as well.

    async function exampleFetch() {
        const response = await fetch('https://reqres.in/api/users/2');
        const json = await response.json();
        console.log(json);
    }
    
    exampleFetch()

    await substitutes for .then(), so when using await fetch, you don't need to use .then() at all.

    Here are a couple other answers which deal with more or less the same issue:

    1 - How can I acces the values of my async fetch function? [duplicate]

    2 - Fetch API Using Async/Await Return Value Unexpected [duplicate]

    0 讨论(0)
提交回复
热议问题