javascript - Assign variable from fetch returning Promise - Stack Overflow

I have two files that are arrays, and i want to load them from a fetch. I have an async function that f

I have two files that are arrays, and i want to load them from a fetch. I have an async function that fetch the files:

async function getData(file) {
    const data = await fetch(`./assets/resources/${file}.json`);
    return await data.json()
}

Then here is where i assign the variables to the return fo this fetch:

let notes = getData("notes").then(res => res)
let pentagrama = getData("pentagrama").then(res => res)

But with this all i get is: from google chrome console

How can i actually get the value?

I have two files that are arrays, and i want to load them from a fetch. I have an async function that fetch the files:

async function getData(file) {
    const data = await fetch(`./assets/resources/${file}.json`);
    return await data.json()
}

Then here is where i assign the variables to the return fo this fetch:

let notes = getData("notes").then(res => res)
let pentagrama = getData("pentagrama").then(res => res)

But with this all i get is: from google chrome console

How can i actually get the value?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Dec 23, 2017 at 17:18 Juan Piza FerraJuan Piza Ferra 1204 silver badges12 bronze badges 2
  • You may have a look at how to return the response from an asynchronous call? – Jonas Wilms Commented Dec 23, 2017 at 17:25
  • Yes thank you, this is the first time that i have to load first a data and use them in the future, i was a little bit confused why did this happened – Juan Piza Ferra Commented Dec 23, 2017 at 17:38
Add a ment  | 

2 Answers 2

Reset to default 5

The result of getData is always a Promise that resolves to your data. To access the values, you can use async/await:

(async () => {

    let notes = await getData("notes");
    let pentagrama = await getData("pentagrama");

    // use them here

})();

Alternatively, you can use Promise.all to wait for both promises to resolve, and then access the received data:

let notesP = getData("notes");
let pentagramaP = getData("pentagrama");

Promise.all([noteP, pentagramaP]).then(res => {

    let notes = res[0];
    let pentagrama = res[1];

    // use them here

});

ASYNC

AWAIT

This will work for you if you just want to check the response in your Google Chrome console because in the console you can use await without an async function which probably could be because everything executed in the console is wrapped in an async function by default(just a speculation).

ONLY WORKS IN CONSOLE:

const getData = (file) => (
  fetch(`./assets/resources/${file}.json`).then(data => data.json());
)

let notes = await getData("notes")
let pentagrama = await getData("pentagrama")

But if you want to get this working in an application, remember that you ALWAYS need to wrap an await inside async

TO GET IT WORKING IN AN APPLICATION:

const getData = async (file) => (
  await fetch(`./assets/resources/${file}.json`).then(data => data.json());
)

const wrapperFunc = async () => {  
  let notes = await getData("notes")
  let pentagrama = await getData("pentagrama")
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745404403a4626248.html

相关推荐

  • javascript - Assign variable from fetch returning Promise - Stack Overflow

    I have two files that are arrays, and i want to load them from a fetch. I have an async function that f

    6小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信