Javascript 면접 질문 설명 - 비동기식 동작

Susan Sarandon
풀어 주다: 2024-10-15 22:38:30
원래의
904명이 탐색했습니다.

Javascript Interview Question Explanation -Asynchronous Behaviour

Asynchronous Behaviour of JavaScript with setTimeout

Introduction

In this article, we will explore a fascinating piece of JavaScript code that demonstrates the asynchronous nature of the language, particularly how closures and the setTimeout function work together. If you've ever been puzzled by why your loop outputs unexpected results, you're in the right place!

Key Concept

Before diving into the code, Let discuss few concept.

Asynchronous Programming:

JavaScript is single-threaded, meaning it can only execute one piece of code at a time. However, it can handle asynchronous operations, allowing certain tasks to run in the background while the main thread continues executing.

SetTimeout

This function is used to execute a piece of code after a specified delay. It takes two parameters: a callback function and a delay in milliseconds.

Closures

A closure is a function that retains access to its lexical scope, even when the function is executed outside that scope. This is crucial for understanding how variables are accessed in asynchronous callbacks.

for (var i = 0; i < 5; i++) {
  setTimeout(function() {
    console.log(i);
  }, i * 1000);
}
로그인 후 복사

Before we dive into the details, take a moment to look at this code snippet. Try to guess what the output will be based on your current understanding. This approach not only helps reinforce your JavaScript skills but also makes the explanation that follows much more meaningful

Think about how JavaScript will process each line. Once you've made your guess, keep reading to see if you got it right!

Explanation

Let break down the code step by step:

Loop Execution : The loop runs five times, incrementing i from 0 to 4.

setTimeout: For each value of i, a setTimeout is scheduled to log i after i * 1000 milliseconds.

Closure: By the time the setTimeout callbacks execute, the loop has already completed, and i has a final value of 5. Therefore, all the callbacks will log 5.

What you might except

0
1
2
3
4
로그인 후 복사
로그인 후 복사

What actually Happens

5
5
5
5
5
로그인 후 복사

However, this is not the actual output that you would see. The reason for this is a common JavaScript behavior related to the scope of the i variable.

In the provided code, the i variable is declared using var, which means it has function scope. When the setTimeout() functions are executed, the loop has already completed, and the value of i is 5. Therefore, all the setTimeout() functions will log the value 5 to the console, regardless of the delay.

Fixing the Issue

To achieve the expected output of 0, 1, 2, 3, 4,

  • you can use an Immediately Invoked Function Expression (IIFE)to create a new scope for each iteration.
  • We can use let keyword instead of var.

Solution

Immediately Invoked Function Expression

for (var i = 0; i < 5; i++) {
  (function(i) {
    setTimeout(function() {
      console.log(i);
    }, i * 1000);
  })(i);
}
로그인 후 복사

Now, each setTimeout will captures the current value of i, and output will be:

0
1
2
3
4
로그인 후 복사
로그인 후 복사

By Using Let keyword

The let keyword creates a block-scoped variable, which means that each iteration of the loop will have its own copy of the i variable, and the setTimeout() functions will capture the correct value of i for each iteration.

Conclusion

Understanding how JavaScript handles asynchronous operations and closures is crucial for writing effective code. The original code snippet serves as a great example of how the setTimeout function interacts with the loop variable i. By using an IIFE, we can ensure that each timeout logs the correct value. So, the next time you encounter a similar situation, remember the power of closures and how they can help you manage asynchronous behaviour in JavaScript

Mission Accomplished: Unraveling the Code!

I hope this explanation not only clarified the code but also sparked some curiosity to explore further. JavaScript is full of surprises and powerful tools, and each piece you learn brings you closer to mastering it.

Thanks for reading

I hope you enjoyed this breakdown! Feel free to share your thoughts, questions, or ideas for future topics in the comments.

Happy Coding

If you enjoyed the article and would like to show your support, be sure to:

Follow me

위 내용은 Javascript 면접 질문 설명 - 비동기식 동작의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!