JavaScript Synchronous vs Asynchronous Programming
Synchronous JavaScript
synchronous means to be in a sequence, i.e. every statement of the code gets executed one by one. So, basically a statement has to wait for the earlier statement to get executed.
A synchronous request blocks the client until operation completes. i.e. browser is unresponsive in such case, javascript engine of the browser is blocked.
Outputconsole.log('Starting')alert('pending');console.log('end')
Starting //show alert('pending') then end
What is that means, JavaScript is a single threaded synchronous programming language. It not allow multiple thing at a time. js code execute line by line. supposed consider this alert, if we are not click the 'ok' button, so next function are not executed, it will stoped. i click 'ok' button then the next line will executed.
Asynchronous JavaScript
Asynchronous code allows the program to be executed immediately where the synchronous code will block further execution of the remaining code until it finishes the current one.
|An asynchronous request doesn't block the client. i.e. browser is responsive.
At that time, user can perform another operations also. In such case, javascript engine of the browser is not blocked.
Outputconsole.log('Starting')setTimeout(()=>{alert('pending');}, 5000);console.log('end')
Starting end //then it will show alert
Post a Comment