0
...
  const a = {
    data: { records: [1, 2, 3] },
  };

  const asyncfunction = (prop) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(prop);
      }, 500);
    });
  };
  const fetch = async () => {
    const res1 = await asyncfunction(a);
    console.log(res1); //output 1,2,3,11111
    a.data.records.push(11111);
  };

  useEffect(() => {
    fetch();
  }, []);

I created a test component to demonstrate this. I thought the console.log will catch the result of await expression immediately and output 1,2,3. How come it becomes 1,2,3,11111 even before I modified it?

ryan
  • 33
  • 4

0 Answers0