Asynchronous programming in JavaScript as of today (continuation of the main topic)
contracts: (...args, callback) => callback(err, data);
You can find code examples in the files mentioned below.
If we want to make a certain time delay.
inside the functiondelay()we use...
- while() / new Date().getTime()
- async () / await / new Promise() / setTimeout()
- new Promise() / setTimeout() / .then()
Here we specify the emulation of asynchronous calls:
- reading of configuration;
- executing SQL query;
- making GET request;
- reading a file;
A callback is a function that must be executed after another function has completed its work.
Imagine that a complex task has to be performed at the enterprise.
We will use
function doСomplexWork(firstTask, secondTask, callback)that accepts three arguments. We will also have two functionsdoTesting()anddoPacking(), that we'll alternate between using as callbacks.
The Promise() constructor takes a function as an argument.
Promises are useful when you have to handle more than one asynchronous task, one after another.
Here we will also use a function
doСomplexWork()with three arguments.
And here.then()will come in handy.