Permalink
6 comments
on commit
sign in to comment.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
util: add util.promisify()
Add `util.promisify(function)` for creating promisified functions. Includes documentation and tests. Fixes: nodejs/CTC#12 PR-URL: #12442 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: William Kapke <william.kapke@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
- Loading branch information
Showing
with
222 additions
and 0 deletions.
- +82 −0 doc/api/util.md
- +61 −0 lib/internal/util.js
- +2 −0 lib/util.js
- +1 −0 src/node_util.cc
- +76 −0 test/parallel/test-util-promisify.js
| @@ -0,0 +1,76 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const fs = require('fs'); | ||
| const vm = require('vm'); | ||
| const { promisify } = require('util'); | ||
|
|
||
| common.crashOnUnhandledRejection(); | ||
|
|
||
| const stat = promisify(fs.stat); | ||
|
|
||
| { | ||
| const promise = stat(__filename); | ||
| assert(promise instanceof Promise); | ||
| promise.then(common.mustCall((value) => { | ||
| assert.deepStrictEqual(value, fs.statSync(__filename)); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| const promise = stat('/dontexist'); | ||
| promise.catch(common.mustCall((error) => { | ||
| assert(error.message.includes('ENOENT: no such file or directory, stat')); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| function fn() {} | ||
| function promisifedFn() {} | ||
| fn[promisify.custom] = promisifedFn; | ||
| assert.strictEqual(promisify(fn), promisifedFn); | ||
| assert.strictEqual(promisify(promisify(fn)), promisifedFn); | ||
| } | ||
|
|
||
| { | ||
| function fn() {} | ||
| fn[promisify.custom] = 42; | ||
| assert.throws( | ||
| () => promisify(fn), | ||
| (err) => err instanceof TypeError && | ||
| err.message === 'The [util.promisify.custom] property must ' + | ||
| 'be a function'); | ||
| } | ||
|
|
||
| { | ||
| const fn = vm.runInNewContext('(function() {})'); | ||
| assert.notStrictEqual(Object.getPrototypeOf(promisify(fn)), | ||
| Function.prototype); | ||
| } | ||
|
|
||
| { | ||
| function fn(callback) { | ||
| callback(null, 'foo', 'bar'); | ||
| } | ||
| promisify(fn)().then(common.mustCall((value) => { | ||
| assert.deepStrictEqual(value, 'foo'); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| function fn(callback) { | ||
| callback(null); | ||
| } | ||
| promisify(fn)().then(common.mustCall((value) => { | ||
| assert.strictEqual(value, undefined); | ||
| })); | ||
| } | ||
|
|
||
| { | ||
| function fn(callback) { | ||
| callback(); | ||
| } | ||
| promisify(fn)().then(common.mustCall((value) => { | ||
| assert.strictEqual(value, undefined); | ||
| })); | ||
| } |
This comment has been minimized.
Will it be in 8.x, then?
This comment has been minimized.
@aendrew Yes! :) Have an RC: https://nodejs.org/download/rc/v8.0.0-rc.0/ The 8.0.0 release itself is planned for/by May 30th. :)
This comment has been minimized.
@aendrew Yes it will, very exciting! I wrote some of my first thoughts on Medium.
This comment has been minimized.
Why is this in the
util.name space and not in thePromisenamespace, which seems far more natural?This comment has been minimized.
Because it’s an utility function. If you have a better place in Node core where it should be (
Promisedoesn’t count for that, see below), we can revisit that. I haven’t seen any, though. :)Because
Promiseis a language built-in, not something that Node offers. We try to avoid messing with built-in object as much as possible, so that users know they may rely on those having the same features available in other environments as Node.This comment has been minimized.
@addaleax Thanks for the prompt and clear response.
I appreciate the attempt to be compatible with other JavaScript environments. I'm part of the group of largely backend-only developers, so I don't notice the compatibility differences with JavaScript engines in browsers, but I do use a mix of native and
bluebirdpromises on the backend. When it comes to comparing tobluebirdand other promise libraries, the choice of theutilnamespace is confusingly different when there's already a precedent forPromise.promisifyexisting and working exactly the same asutil.promisify().What would be more useful to be would be:
This solution be non-conflicting-- an understandable goal-- but still clear and convenient. Loading most Promise methods from
Promisebut then loading just one fromutil.would be both confusing and inconsistent.In my example, I use the new
node.namespace for a sub-namespace that might conflict with a native namespace, but that has been modified or extended for Node.js.