Skip to content

Commit c3783a7

Browse files
committed
minor
1 parent 9f98090 commit c3783a7

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

1-js/11-async/06-promisify/article.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
Promisification -- is a long word for a simple transform. It's conversion of a function that accepts a callback into a function returning a promise.
44

5-
To be more precise, we create a wrapper-function that does the same, internally calling the original one, but returns a promise.
6-
75
Such transforms are often needed in real-life, as many functions and libraries are callback-based. But promises are more convenient. So it makes sense to promisify those.
86

97
For instance, we have `loadScript(src, callback)` from the chapter <info:callbacks>.
@@ -23,7 +21,7 @@ function loadScript(src, callback) {
2321
// loadScript('path/script.js', (err, script) => {...})
2422
```
2523

26-
Let's promisify it. The new `loadScriptPromise(src)` function will do the same, but accept only `src` (no callback) and return a promise.
24+
Let's promisify it. The new `loadScriptPromise(src)` function will do the same, but accept only `src` (no `callback`) and return a promise.
2725

2826
```js
2927
let loadScriptPromise = function(src) {
@@ -39,13 +37,13 @@ let loadScriptPromise = function(src) {
3937
// loadScriptPromise('path/script.js').then(...)
4038
```
4139

42-
Now `loadScriptPromise` fits well in our promise-based code.
40+
Now `loadScriptPromise` fits well in promise-based code.
4341

4442
As we can see, it delegates all the work to the original `loadScript`, providing its own callback that translates to promise `resolve/reject`.
4543

46-
As we may need to promisify many functions, it makes sense to use a helper.
44+
In practice we'll probably need to promisify many functions, it makes sense to use a helper.
4745

48-
That's actually very simple -- `promisify(f)` below takes a to-promisify function `f` and returns a wrapper function.
46+
We'll call it `promisify(f)`: it accepts a to-promisify function `f` and returns a wrapper function.
4947

5048
That wrapper does the same as in the code above: returns a promise and passes the call to the original `f`, tracking the result in a custom callback:
5149

@@ -61,7 +59,7 @@ function promisify(f) {
6159
}
6260
}
6361

64-
args.push(callback); // append our custom callback to the end of arguments
62+
args.push(callback); // append our custom callback to the end of f arguments
6563

6664
f.call(this, ...args); // call the original function
6765
});
@@ -75,9 +73,9 @@ loadScriptPromise(...).then(...);
7573

7674
Here we assume that the original function expects a callback with 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.
7775

78-
But what if the original `f` expects a callback with more arguments `callback(err, res1, res2)`?
76+
But what if the original `f` expects a callback with more arguments `callback(err, res1, res2, ...)`?
7977

80-
Here's a modification of `promisify` that returns an array of multiple callback results:
78+
Here's a more advanced version of `promisify`: if called as `promisify(f, true)`, the promise result will be an array of callback results `[res1, res2, ...]`:
8179

8280
```js
8381
// promisify(f, true) to get array of results
@@ -105,7 +103,7 @@ f = promisify(f, true);
105103
f(...).then(arrayOfResults => ..., err => ...)
106104
```
107105
108-
In some cases, `err` may be absent at all: `callback(result)`, or there's something exotic in the callback format, then we can promisify such functions without using the helper, manually.
106+
For more exotic callback formats, like those without `err` at all: `callback(result)`, we can promisify such functions without using the helper, manually.
109107
110108
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.
111109

0 commit comments

Comments
 (0)