You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"Promisification" is a long word for a simple transformation. It's the conversion of a function that accepts a callback into a function that returns a promise.
3
+
"Промісифікація" -- це довге слово для простої трансформації. Це перетворення функції, яка приймає колбек та повертає проміс.
4
4
5
-
Such transformations are often required in real-life, as many functions and libraries are callback-based. But promises are more convenient, so it makes sense to promisify them.
5
+
Такі перетворення часто необхідні в реальному житті, оскільки багато функцій та бібліотеки засновані на колбеках, а використання промісів зручніше, тому є сенс «промісифікувати» їх.
6
6
7
-
For better understanding, let's see an example.
7
+
Для кращого розуміння розглянемо приклад.
8
8
9
-
For instance, we have `loadScript(src, callback)`from the chapter<info:callbacks>.
9
+
Ми візьмемо `loadScript(src, callback)`з розділу<info:callbacks>.
The function loads a script with the given `src`, and then calls`callback(err)`in case of an error, or `callback(null, script)`in case of successful loading. That's a widespread agreement for using callbacks, we saw it before.
26
+
Функція завантажує скрипт використовуючи аргумент `src`, а потім викликає`callback(err)`у випадку помилки чи `callback(null, script)`у випадку успішного завантаження. Це усім відоме використання колбеку, яке ми вже бачили.
27
27
28
-
Let's promisify it.
28
+
Давайте промісифікуємо цю функцію.
29
29
30
-
We'll make a new function `loadScriptPromise(src)`, that does the same (loads the script), but returns a promise instead of using callbacks.
30
+
Створимо нову функцію `loadScriptPromise(src)`, яка робить те саме (завантажує скрипт), але повертає проміс замість використання колбеку.
31
31
32
-
In other words, we pass it only`src` (no`callback`) and get a promise in return, that resolves with `script`when the load is successful, and rejects with the error otherwise.
32
+
Іншими словами, ми будемо передавати тільки`src` (не`callback`) і отримаємо проміс у відповіді, який поверне `script`коли завантаження успішне, і помилку, якщо ні.
33
33
34
-
Here it is:
34
+
Реалізація такої функції:
35
35
```js
36
36
letloadScriptPromise=function(src) {
37
37
returnnewPromise((resolve, reject) => {
@@ -42,65 +42,65 @@ let loadScriptPromise = function(src) {
42
42
});
43
43
};
44
44
45
-
//usage:
45
+
//використання:
46
46
// loadScriptPromise('path/script.js').then(...)
47
47
```
48
48
49
-
As we can see, the new function is a wrapper around the original `loadScript`function. It calls it providing its own callback that translates to promise`resolve/reject`.
49
+
Як ви можете бачити, нова функція -- це обгортка оригінальної `loadScript`функції. Вона викликає власний колбек, який працює з функціями проміса`resolve/reject`.
50
50
51
-
Now `loadScriptPromise`fits well in promise-based code. If we like promises more than callbacks (and soon we'll see more reasons for that), then we will use it instead.
51
+
Як бачимо, функція `loadScriptPromise`добре вписується в асинхронну поведінку промісів.
52
52
53
-
In practice we may need to promisify more than one function, so it makes sense to use a helper.
53
+
На практиці нам, швидше за все, знадобиться промісифікувати не одну функцію, тому є сенс зробити для цього спеціальну «функцію-помічник».
54
54
55
-
We'll call it`promisify(f)`: it accepts a to-promisify function `f`and returns a wrapper function.
55
+
Ми назвемо її`promisify(f)` -- вона приймає функцію для промісифікації `f`та повертає функцію-обгортку.
56
56
57
57
```js
58
58
functionpromisify(f) {
59
-
returnfunction (...args) { //return a wrapper-function (*)
functioncallback(err, result) { //our custom callback for f (**)
61
+
functioncallback(err, result) { //наш спеціальний колбек для f (**)
62
62
if (err) {
63
63
reject(err);
64
64
} else {
65
65
resolve(result);
66
66
}
67
67
}
68
68
69
-
args.push(callback); //append our custom callback to the end of f arguments
69
+
args.push(callback); //додаємо колбек у кінець аргументів f
70
70
71
-
f.call(this, ...args); //call the original function
71
+
f.call(this, ...args); //викликаємо оригінальну функцію
72
72
});
73
73
};
74
74
}
75
75
76
-
//usage:
76
+
//використання:
77
77
let loadScriptPromise =promisify(loadScript);
78
78
loadScriptPromise(...).then(...);
79
79
```
80
80
81
-
The code may look a bit complex, but it's essentially the same that we wrote above, while promisifying`loadScript` function.
81
+
Код може виглядати дещо складним, але по суті він такий самий, як ми написали вище, промісифікуючи функцію`loadScript`.
82
82
83
-
A call to `promisify(f)`returns a wrapper around `f``(*)`. That wrapper returns a promise and forwards the call to the original `f`, tracking the result in the custom callback`(**)`.
83
+
Виклик функції `promisify(f)`поверне функцію-обгортку для `f``(*)`. Ця обгортка повертає проміс і викликає оригінальну функцію `f`, відстежуючи результат у спеціальному зворотному виклику`(**)`.
84
84
85
-
Here, `promisify`assumes that the original function expects a callback with exactly two arguments `(err, result)`. That's what we encounter most often. Then our custom callback is in exactly the right format, and `promisify` works great for such a case.
85
+
В цьому випадку, `promisify`припускає, що оригінальна функція очікує колбек тільки з двома аргументами `(err, result)`. З таким результатом колбеку ви працюватимете найчастіше. В такому випадку наш колбек написаний і відпрацьовуватиме правильно.
86
86
87
-
But what if the original`f`expects a callback with more arguments`callback(err, res1, res2, ...)`?
87
+
Але що, якщо вихідна функція`f`очікує колбек з більшою кількістю аргументів`callback(err, res1, res2, ...)`?
88
88
89
-
We can improve our helper. Let's make a more advanced version of`promisify`.
89
+
Ми можемо покращити нашу функцію-помічник. Зробімо розширену версію`promisify`.
90
90
91
-
-When called as `promisify(f)`it should work similar to the version above.
92
-
-When called as `promisify(f, true)`, it should return the promise that resolves with the array of callback results. That's exactly for callbacks with many arguments.
91
+
-Викличмо функцію `promisify(f)`з одним аргументом, то вона повинна працювати як і раніше.
92
+
-Викличмо функцію `promisify(f, true)` з двома аргументами, яка повинна повернути проміс, який поверне масив результатів з колбеку. Те ж саме повинно відбуватись для колбеку з багатьма аргументами.
93
93
94
94
```js
95
-
// promisify(f, true) to get array of results
95
+
// promisify(f, true) повинна повернути масив результатів
function*!*callback(err, ...results*/!*) { //наш спеціальний колбек дляf
100
100
if (err) {
101
101
reject(err);
102
102
} else {
103
-
//resolve with all callback results if manyArgs is specified
103
+
//повернемо для всі результати колбека, якщо задано значення manyArgs === true
104
104
*!*resolve(manyArgs ? results : results[0]);*/!*
105
105
}
106
106
}
@@ -112,21 +112,21 @@ function promisify(f, manyArgs = false) {
112
112
};
113
113
}
114
114
115
-
//usage:
115
+
//використання:
116
116
f =promisify(f, true);
117
117
f(...).then(arrayOfResults=>..., err=>...);
118
118
```
119
119
120
-
As you can see it's essentially the same as above, but`resolve`is called with only one or all arguments depending on whether `manyArgs`is truthy.
120
+
Як ви можете бачити, це по суті те саме, що й вище, але`resolve`викликається лише з одним або з усіма аргументами залежно від того, чи є `manyArgs`істинним.
121
121
122
-
For more exotic callback formats, like those without `err` at all: `callback(result)`, we can promisify such functions manually without using the helper.
122
+
Для більш екзотичних форматів колбека, наприклад, без `err`: `callback(result)`, ми можемо промісифікувати функції без помічника, «вручну».
123
123
124
-
There are also modules with a bit more flexible promisification functions, e.g. [es6-promisify](https://github.com/digitaldesignlabs/es6-promisify). In Node.js, there's a built-in `util.promisify`function for that.
124
+
Існують також модулі з більш гнучкою промісифікацією, наприклад, [es6-promisify](https://github.com/digitaldesignlabs/es6-promisify) або вбудована функція `util.promisify`в Node.js.
125
125
126
126
```smart
127
-
Promisification is a great approach, especially when you use `async/await` (see the next chapter), but not a total replacement for callbacks.
127
+
Промісифікація –- це чудовий підхід, особливо якщо ви будете використовувати `async/await` (дивіться наступний розділ), але вона не є повноцінно заміною будь-яких колбеків.
128
128
129
-
Remember, a promise may have only one result, but a callback may technically be called many times.
129
+
Пам'ятайте, проміс може мати лише один результат, але колбек технічно може викликатися скільки завгодно разів.
130
130
131
-
So promisification is only meant for functions that call the callback once. Further calls will be ignored.
131
+
Тому промісифікація використовується для функцій, що викликають колбек лише один раз. Наступні виклики колбека будуть проігноровані.
0 commit comments