Why does alert(); run before console.log();

后端 未结 2 1812
半阙折子戏
半阙折子戏 2020-12-20 16:08

How My Question is Different From Others

I am using ES6 syntax. The other questions I looked at uses ES5 syntax.

The Question

2条回答
  •  一整个雨季
    2020-12-20 16:36

    Check test below, which will return from function on 999 cycle of for loop before window.alert function fires. In my case I did not see window.alert. This is because actually alert runs after loop of console.log's functions.

    // Test function
    const test = (br) => {
      for (let i = 0; i < 1000; i++) {
        console.log(i);
        // If br true and cycle #999 - return before
        // loop end and window.alert exec
        if(br && i === 999) return;
      }
      window.alert('Hello World!');
    };
    
    // Test
    test(true);

提交回复
热议问题