From 58398a6586503c8417f5b7ff55c159d0c394b770 Mon Sep 17 00:00:00 2001 From: Jonathan Chue <5141427+jchue@users.noreply.github.com> Date: Sat, 7 Dec 2019 16:51:01 -0800 Subject: [PATCH] Make minor grammar corrections/updates to async/promisify --- 1-js/11-async/06-promisify/article.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/1-js/11-async/06-promisify/article.md b/1-js/11-async/06-promisify/article.md index 6d91c049..66e8a222 100644 --- a/1-js/11-async/06-promisify/article.md +++ b/1-js/11-async/06-promisify/article.md @@ -1,8 +1,8 @@ # Promisification -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. +"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. -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. +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. For instance, we have `loadScript(src, callback)` from the chapter . @@ -21,7 +21,7 @@ function loadScript(src, callback) { // loadScript('path/script.js', (err, script) => {...}) ``` -Let's promisify it. The new `loadScriptPromise(src)` function will do the same, but accept only `src` (no `callback`) and return a promise. +Let's promisify it. The new `loadScriptPromise(src)` function achieves the same result, but it accepts only `src` (no `callback`) and returns a promise. ```js let loadScriptPromise = function(src) { @@ -41,9 +41,7 @@ Now `loadScriptPromise` fits well in promise-based code. As we can see, it delegates all the work to the original `loadScript`, providing its own callback that translates to promise `resolve/reject`. -In practice we'll probably need to promisify many functions, it makes sense to use a helper. - -We'll call it `promisify(f)`: it accepts a to-promisify function `f` and returns a wrapper function. +In practice we'll probably need to promisify many functions, so it makes sense to use a helper. We'll call it `promisify(f)`: it accepts a to-promisify function `f` and returns a wrapper function. 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: @@ -103,7 +101,7 @@ f = promisify(f, true); f(...).then(arrayOfResults => ..., err => ...) ``` -For more exotic callback formats, like those without `err` at all: `callback(result)`, we can promisify such functions without using the helper, manually. +For more exotic callback formats, like those without `err` at all: `callback(result)`, we can promisify such functions manually without using the helper. 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.