I
I
ikutin6662021-11-15 20:55:23
JavaScript
ikutin666, 2021-11-15 20:55:23

Why use writable._construct(callback)?

Why implement writable._construct(callback)?

according to documentation

This optional function will be called in a tick after the stream constructor has returned, delaying any _write(), _final() and _destroy() calls until callback is called. This is useful to initialize state or asynchronously initialize resources before the stream can be used.


But what does that mean))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Akylbek Omar, 2021-11-16
@Aboba45849894855

The callback concept has many applications. For example, some algorithms (functions) have as a subtask the task of calculating a hash value from a string. In the arguments when starting the algorithm (function), it is convenient to specify which function to use to calculate the hash values.
Another example of an algorithm for which it is natural to pass a function as an argument is an algorithm for traversing some object store, applying some action to each object. A callback can act as this action (algorithm).
The callback programming technique in programming languages ​​like C is simple. When the main function is called, it is simply passed a pointer to the callback function. The classic example is the qsort function from the stdlib library. This function allows you to sort an array of byte blocks of the same length. As arguments, it receives the address of the first element of the array, the number of blocks in the array, the size of the block of bytes, and a pointer to a function for comparing two blocks of bytes. This comparison function is the callback function in this example:
`#include
// modulo integer comparison function
int compare_abs(const void *a, const void *b) {
int a1 = *(int*)a;
int b1 = *(int*)b;
return abs(a1) - abs(b1);
}
int main() {
int size = 10;
int m[size] = {1, -3, 5, -100, 7, 33, 44, 67, -4, 0};
// sorting the array m in ascending modules
qsort(m, size, sizeof(int), compare_abs);
return 0;
}`
You can think of a callback as an action passed as an argument to some main procedure. And this action can be considered as: a
subtask and be used to process data within this procedure;
“Telephone connection” used to “contact” with the one who called the procedure when some event occurs (English callback literally translates as “call back”).
The example shown above corresponds exactly to the first case. The case where the callback is used as a "telephone" reflects the code where the function to handle a certain signal is specified:
`#include
#include
volatile sig_atomic_t br = 1;
void sig(int signum)
{
br=0;
}
int main(int argc, char *argv[])
{
signal(SIGINT, sig);
printf("Press break keyboard key combination to stop the program\n");
while(br);
printf("Received SIGINT, exit\n");
return 0;
}`
In some programming languages, such as Common Lisp, Erlang, Scheme, Clojure, PHP, JavaScript, Perl, Python, Ruby and others, it is possible to construct anonymous (unnamed) functions and closure functions right in the main function call expression, and this opportunity is widely used.
In AJAX technology, when making an asynchronous request to the server, you must specify a callback function that will be called as soon as the response to the request arrives. Often this function is defined "in place" without giving it any specific name:
`new Ajax.Request(' example.com/do_it ',
{
method: 'post',
onSuccess: function(transport) { // function called
window.alert("Done!"); // upon successful completion of the request
}, //
onFailure: function() { // function called
window.alert("Error!"); // on request execution error
}
});`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question