In the code below, I find the console.log("resolve")
will be executed two times when I run this code in React. But when I run this code in browser console, there is no problem. Why this happens, I am so confused. Hope you can give me some help, thanks!
export const Wei = () => {
const myFun = () => {
let state = false;
setTimeout(() => {
state = true;
}, 2000);
return new Promise((resolve, reject) => {
setTimeout(() => {
if (state) {
console.log("resolve");
resolve("State is true");
}
else {
reject("State is false");
}
}, 3000);
});
};
console.log("render");
const getResult = async () => {
console.log("before");
const res = await myFun();
console.log("after");
return res;
};
console.log(getResult());
return <div>1</div>;
};