Skip to content

Commit a8283a4

Browse files
authored
Promisification (#229)
1 parent 598b4a4 commit a8283a4

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed
Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
# Promisification
1+
# Промісифікація
22

3-
"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+
"Промісифікація" -- це довге слово для простої трансформації. Це перетворення функції, яка приймає колбек та повертає проміс.
44

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+
Такі перетворення часто необхідні в реальному житті, оскільки багато функцій та бібліотеки засновані на колбеках, а використання промісів зручніше, тому є сенс «промісифікувати» їх.
66

7-
For better understanding, let's see an example.
7+
Для кращого розуміння розглянемо приклад.
88

9-
For instance, we have `loadScript(src, callback)` from the chapter <info:callbacks>.
9+
Ми візьмемо `loadScript(src, callback)` з розділу <info:callbacks>.
1010

1111
```js run
1212
function loadScript(src, callback) {
1313
let script = document.createElement('script');
1414
script.src = src;
1515

1616
script.onload = () => callback(null, script);
17-
script.onerror = () => callback(new Error(`Script load error for ${src}`));
17+
script.onerror = () => callback(new Error(`Помилка завантаження скрипта ${src}`));
1818

1919
document.head.append(script);
2020
}
2121

22-
// usage:
22+
// використання:
2323
// loadScript('path/script.js', (err, script) => {...})
2424
```
2525

26-
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)` у випадку успішного завантаження. Це усім відоме використання колбеку, яке ми вже бачили.
2727

28-
Let's promisify it.
28+
Давайте промісифікуємо цю функцію.
2929

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)`, яка робить те саме (завантажує скрипт), але повертає проміс замість використання колбеку.
3131

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` коли завантаження успішне, і помилку, якщо ні.
3333

34-
Here it is:
34+
Реалізація такої функції:
3535
```js
3636
let loadScriptPromise = function(src) {
3737
return new Promise((resolve, reject) => {
@@ -42,65 +42,65 @@ let loadScriptPromise = function(src) {
4242
});
4343
};
4444

45-
// usage:
45+
// використання:
4646
// loadScriptPromise('path/script.js').then(...)
4747
```
4848

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`.
5050

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` добре вписується в асинхронну поведінку промісів.
5252

53-
In practice we may need to promisify more than one function, so it makes sense to use a helper.
53+
На практиці нам, швидше за все, знадобиться промісифікувати не одну функцію, тому є сенс зробити для цього спеціальну «функцію-помічник».
5454

55-
We'll call it `promisify(f)`: it accepts a to-promisify function `f` and returns a wrapper function.
55+
Ми назвемо її `promisify(f)` -- вона приймає функцію для промісифікації `f` та повертає функцію-обгортку.
5656

5757
```js
5858
function promisify(f) {
59-
return function (...args) { // return a wrapper-function (*)
59+
return function (...args) { // повертає функію-обгортку (*)
6060
return new Promise((resolve, reject) => {
61-
function callback(err, result) { // our custom callback for f (**)
61+
function callback(err, result) { // наш спеціальний колбек для f (**)
6262
if (err) {
6363
reject(err);
6464
} else {
6565
resolve(result);
6666
}
6767
}
6868

69-
args.push(callback); // append our custom callback to the end of f arguments
69+
args.push(callback); // додаємо колбек у кінець аргументів f
7070

71-
f.call(this, ...args); // call the original function
71+
f.call(this, ...args); // викликаємо оригінальну функцію
7272
});
7373
};
7474
}
7575

76-
// usage:
76+
// використання:
7777
let loadScriptPromise = promisify(loadScript);
7878
loadScriptPromise(...).then(...);
7979
```
8080

81-
The code may look a bit complex, but it's essentially the same that we wrote above, while promisifying `loadScript` function.
81+
Код може виглядати дещо складним, але по суті він такий самий, як ми написали вище, промісифікуючи функцію `loadScript`.
8282

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`, відстежуючи результат у спеціальному зворотному виклику `(**)`.
8484

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)`. З таким результатом колбеку ви працюватимете найчастіше. В такому випадку наш колбек написаний і відпрацьовуватиме правильно.
8686

87-
But what if the original `f` expects a callback with more arguments `callback(err, res1, res2, ...)`?
87+
Але що, якщо вихідна функція `f` очікує колбек з більшою кількістю аргументів `callback(err, res1, res2, ...)`?
8888

89-
We can improve our helper. Let's make a more advanced version of `promisify`.
89+
Ми можемо покращити нашу функцію-помічник. Зробімо розширену версію `promisify`.
9090

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)` з двома аргументами, яка повинна повернути проміс, який поверне масив результатів з колбеку. Те ж саме повинно відбуватись для колбеку з багатьма аргументами.
9393

9494
```js
95-
// promisify(f, true) to get array of results
95+
// promisify(f, true) повинна повернути масив результатів
9696
function promisify(f, manyArgs = false) {
9797
return function (...args) {
9898
return new Promise((resolve, reject) => {
99-
function *!*callback(err, ...results*/!*) { // our custom callback for f
99+
function *!*callback(err, ...results*/!*) { // наш спеціальний колбек для f
100100
if (err) {
101101
reject(err);
102102
} else {
103-
// resolve with all callback results if manyArgs is specified
103+
// повернемо для всі результати колбека, якщо задано значення manyArgs === true
104104
*!*resolve(manyArgs ? results : results[0]);*/!*
105105
}
106106
}
@@ -112,21 +112,21 @@ function promisify(f, manyArgs = false) {
112112
};
113113
}
114114

115-
// usage:
115+
// використання:
116116
f = promisify(f, true);
117117
f(...).then(arrayOfResults => ..., err => ...);
118118
```
119119
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` істинним.
121121
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)`, ми можемо промісифікувати функції без помічника, «вручну».
123123
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.
125125
126126
```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` (дивіться наступний розділ), але вона не є повноцінно заміною будь-яких колбеків.
128128

129-
Remember, a promise may have only one result, but a callback may technically be called many times.
129+
Пам'ятайте, проміс може мати лише один результат, але колбек технічно може викликатися скільки завгодно разів.
130130
131-
So promisification is only meant for functions that call the callback once. Further calls will be ignored.
131+
Тому промісифікація використовується для функцій, що викликають колбек лише один раз. Наступні виклики колбека будуть проігноровані.
132132
```

0 commit comments

Comments
 (0)