Skip to content

Commit

Permalink
doc: add colorText
Browse files Browse the repository at this point in the history
  • Loading branch information
hemanth committed Jun 14, 2022
1 parent 7cd400f commit 01a77c8
Showing 1 changed file with 9 additions and 45 deletions.
54 changes: 9 additions & 45 deletions doc/api/util.md
Expand Up @@ -16,59 +16,23 @@ const util = require('node:util');

## `util.callbackify(original)`

## `util.colorText(format, text)`

<!-- YAML
added: v8.2.0
added: v18.3.0
-->

* `original` {Function} An `async` function
* Returns: {Function} a callback style function
* `format` {String} `format` one of the color format from `util.inspect.colors`
* `text` {String} The text you would like to color
* Returns: {string} colored text string

Takes an `async` function (or a function that returns a `Promise`) and returns a
function following the error-first callback style, i.e. taking
an `(err, value) => ...` callback as the last argument. In the callback, the
first argument will be the rejection reason (or `null` if the `Promise`
resolved), and the second argument will be the resolved value.
Takes `format` and `text` and retuns the colored text form

```js
const util = require('node:util');

async function fn() {
return 'hello world';
}
const callbackFunction = util.callbackify(fn);

callbackFunction((err, ret) => {
if (err) throw err;
console.log(ret);
});
```

Will print:

```text
hello world
```

The callback is executed asynchronously, and will have a limited stack trace.
If the callback throws, the process will emit an [`'uncaughtException'`][]
event, and if not handled will exit.

Since `null` has a special meaning as the first argument to a callback, if a
wrapped function rejects a `Promise` with a falsy value as a reason, the value
is wrapped in an `Error` with the original value stored in a field named
`reason`.

```js
function fn() {
return Promise.reject(null);
}
const callbackFunction = util.callbackify(fn);

callbackFunction((err, ret) => {
// When the Promise was rejected with `null` it is wrapped with an Error and
// the original value is stored in `reason`.
err && Object.hasOwn(err, 'reason') && err.reason === null; // true
});
console.log(util.colorText('red', 'This text shall be in red color'));
// ^ '\u001b[31mThis text shall be in red color\u001b[39m'
```

## `util.debuglog(section[, callback])`
Expand Down

0 comments on commit 01a77c8

Please sign in to comment.