admin管理员组文章数量:1023815
JavaScript code runs on a single thread, in event loop when method push to web apis return callback function to queue, listener queue and callstack is empty push callback function execute it in callstack, so the asynchronous execution is really just changing the execution order of the functions? Why?
JavaScript code runs on a single thread, in event loop when method push to web apis return callback function to queue, listener queue and callstack is empty push callback function execute it in callstack, so the asynchronous execution is really just changing the execution order of the functions? Why?
Share Improve this question asked Apr 28, 2022 at 1:20 DANG PHONGDANG PHONG 112 bronze badges 1- Please provide enough code so others can better understand or reproduce the problem. – Community Bot Commented Apr 28, 2022 at 14:17
1 Answer
Reset to default 8Due to JavaScript being single-threaded, it is technically only concurrent, not parallel. That is, no two instructions are ever executed at the same time, but the event loop allows us to coordinate the order in which instructions are executed such that certain instructions are non-blocking.
To see why this is useful, consider the following sequence of steps:
- Make a call to an API
- Do some other work
Step 1 could take quite some time, and we may want to do some other work in the meantime while we wait for it to plete, making it a blocking call. In a purely sequential world, this means that step 2 will not execute until step 1 has been pleted. In parison, if we make this asynchronous, and therefore non-blocking, we can execute steps 1 and 2 concurrently. This happens because the event loop will run an iteration of our synchronous code first (step 2), then run our asynchronous code (step 1) - it is changing the order instructions are executed in such a way that our code is non-blocking. Interestingly, this means an asynchronous delay of 0 seconds will run after the next synchronous iteration of code.
Say we introduce step 3, in which we need the response from the API call in step 1. To do this, we can use an async-await, which will be blocking until step 1 pletes. This is not purely sequential, as Step 2 does not need to wait for step 1 to plete, but at step 3 we are able to create a synchronous point in our code via async-await. So we are able to keep our code non-blocking where it matters, and still introduce a particular sequence of events.
JavaScript code runs on a single thread, in event loop when method push to web apis return callback function to queue, listener queue and callstack is empty push callback function execute it in callstack, so the asynchronous execution is really just changing the execution order of the functions? Why?
JavaScript code runs on a single thread, in event loop when method push to web apis return callback function to queue, listener queue and callstack is empty push callback function execute it in callstack, so the asynchronous execution is really just changing the execution order of the functions? Why?
Share Improve this question asked Apr 28, 2022 at 1:20 DANG PHONGDANG PHONG 112 bronze badges 1- Please provide enough code so others can better understand or reproduce the problem. – Community Bot Commented Apr 28, 2022 at 14:17
1 Answer
Reset to default 8Due to JavaScript being single-threaded, it is technically only concurrent, not parallel. That is, no two instructions are ever executed at the same time, but the event loop allows us to coordinate the order in which instructions are executed such that certain instructions are non-blocking.
To see why this is useful, consider the following sequence of steps:
- Make a call to an API
- Do some other work
Step 1 could take quite some time, and we may want to do some other work in the meantime while we wait for it to plete, making it a blocking call. In a purely sequential world, this means that step 2 will not execute until step 1 has been pleted. In parison, if we make this asynchronous, and therefore non-blocking, we can execute steps 1 and 2 concurrently. This happens because the event loop will run an iteration of our synchronous code first (step 2), then run our asynchronous code (step 1) - it is changing the order instructions are executed in such a way that our code is non-blocking. Interestingly, this means an asynchronous delay of 0 seconds will run after the next synchronous iteration of code.
Say we introduce step 3, in which we need the response from the API call in step 1. To do this, we can use an async-await, which will be blocking until step 1 pletes. This is not purely sequential, as Step 2 does not need to wait for step 1 to plete, but at step 3 we are able to create a synchronous point in our code via async-await. So we are able to keep our code non-blocking where it matters, and still introduce a particular sequence of events.
本文标签: asynchronousIs async in javascript concurrent or parallelStack Overflow
版权声明:本文标题:asynchronous - Is async in javascript concurrent or parallel? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745592358a2157964.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论