Reputation: 55
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
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