manuel_m2
manuel_m2

Reputation: 55

How I can pass nested promises to async await

I have a function that downloads a file from endpoint using filesaver.js the problem is that I need to pass the function to async await method

fileDownload = () => {
    fetch('http://localhost:3000/api/buildings/1/assets.xlsx')
        .then(res => res.blob())
        .then(blob => saveAs(blob, 'assets.xlsx'))
}

How can change this to async await with this nested promises? (the function is ok)

Upvotes: 3

Views: 1062

Answers (1)

Bharat23
Bharat23

Reputation: 537

You can use the code:

fileDownload = async () => {
    let response = await fetch('http://localhost:3000/api/buildings/1/assets.xlsx');
    let blob = await response.blob();
    await saveAs(blob, 'assets.xlxs');
};

Upvotes: 3

Related Questions