G
G
george os2019-11-22 11:04:10
JavaScript
george os, 2019-11-22 11:04:10

Why async?

I understand that asynchrony is mainly used to communicate with the network, be it POST or GET
, but what else can it be used for?
Especially when it comes to async in Redux?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya, 2019-11-22
@horhik

Asynchronous code execution allows more efficient use of processor time.
If we take a synchronous execution scenario, then for some time the application thread loads the processor with work, then there usually comes a situation when the application thread waits for the completion of some I / O operation. This may be waiting for a timer to fire, waiting for data from a network card, waiting for user input, etc. While the wait is in progress, the application thread is not using the processor. In some cases, this suits us if we cannot do other useful work. In other situations, we want to do something at the same time, for example, to be able to handle user clicks while a database query is in progress, for example.
Then we useasynchronous approach. In this case, when the application thread reaches the end of the I / O operation, it does not wait and can be used for other work, and at the moment when the I / O operation ends and the thread is not busy with other work, it will be able to continue executing the code from that the place where he stopped.
There is also another concept of asynchrony, also called deferred execution. This is a situation where the data is not processed at the time of the user's request, but the data is quickly buffered on disk in the database or queue and we quickly respond to the user. And at this moment, the asynchronous worker cyclically processes this buffer, for example, reduces pictures, or recodes the video, or calculates some kind of aggregation.

W
WinPooh32, 2019-11-22
@WinPooh32

but what else can it be used for?

For example, brewing tea by a person:
Turn on the kettle with water. While the water is heating, put the tea bag and sugar in a mug. Here you can start washing dishes. When the kettle turns off, pour into a mug and stir. Continue washing dishes.
And if you do everything in sync, you will have to wait and do nothing until the kettle heats up, then make tea and wash the dishes. Obviously this is not efficient.
Replace a person with a stream, a kettle of water with network requests, a mug of tea, dishes with data processing.

X
xmoonlight, 2019-11-22
@xmoonlight

To increase productivity: the speed of data processing per unit of time.
For example, it is for this reason that the browser loads content asynchronously if possible.
And they build auto-roads in several lanes.
So that independent processes do not wait for the start and completion of each other's execution, but use all available free resources to increase overall performance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question