Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  33] [ 7]  / answers: 1 / hits: 5569  / 3 Years ago, tue, august 17, 2021, 12:00:00
const loadUsers = async () => {
setTimeout(() => {
showLoader();

const response = await fetch("https://reqres.in/api/users?page=1");
let userData = (await response.json()).data;

setAllUsers(userData);
setUsers(userData);

hideLoader();

}, 3000);
};

More From » reactjs

 Answers
1

If you want to use async and await, the innermost function that contains the await statement is required to have the async keyword.



Try this.



const loadUsers = () => {
setTimeout(async () => {
showLoader();

const response = await fetch("https://reqres.in/api/users?page=1");
let userData = (await response.json()).data;

setAllUsers(userData);
setUsers(userData);

hideLoader();

}, 3000);
};

[#985] Monday, August 9, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaredsages

Total Points: 273
Total Questions: 97
Total Answers: 105

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
;