Permalink
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 callbackify
Add `util.callbackify(function)` for creating callback style functions from functions returning a `Thenable` PR-URL: #12712 Fixes: nodejs/CTC#109 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
- Loading branch information
Showing
with
374 additions
and 0 deletions.
- +59 −0 doc/api/util.md
- +1 −0 lib/internal/errors.js
- +50 −0 lib/util.js
- +15 −0 test/fixtures/uncaught-exceptions/callbackify1.js
- +22 −0 test/fixtures/uncaught-exceptions/callbackify2.js
- +227 −0 test/parallel/test-util-callbackify.js
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
// Used to test that `uncaughtException` is emitted | ||
|
||
const { callbackify } = require('util'); | ||
|
||
{ | ||
async function fn() { } | ||
|
||
const cbFn = callbackify(fn); | ||
|
||
cbFn((err, ret) => { | ||
throw new Error(__filename); | ||
}); | ||
} |
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
// Used to test the `uncaughtException` err object | ||
|
||
const assert = require('assert'); | ||
const { callbackify } = require('util'); | ||
|
||
{ | ||
const sentinel = new Error(__filename); | ||
process.once('uncaughtException', (err) => { | ||
assert.strictEqual(err, sentinel); | ||
// Calling test will use `stdout` to assert value of `err.message` | ||
console.log(err.message); | ||
}); | ||
|
||
async function fn() { | ||
return await Promise.reject(sentinel); | ||
} | ||
|
||
const cbFn = callbackify(fn); | ||
cbFn((err, ret) => assert.ifError(err)); | ||
} |
Oops, something went wrong.