forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `util.promisify(function)` for creating promisified functions. Includes documentation and tests. Fixes: nodejs/CTC#12 PR-URL: nodejs#12442 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: William Kapke <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Teddy Katz <[email protected]>
- Loading branch information
Showing
5 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
})); | ||
} |