Node Utils Module
npm install @neo9/n9-node-utilsNeo9 Node Utils Module.




``bash`
npm install --save @neo9/n9-node-utils
or
`bash`
yarn add --dev @neo9/n9-node-utils
- Drop Node 8 support
- Rename N9JsonStreamResponse to N9JSONStreamResponse
- N9Error
- ok
- cb
- waitFor
- waitForEvent
- asyncObject
Custom error class (extends Error), used by n9-node-micro for API errors with status code and context.
`ts`
new N9Error(message [, statusCode] [, context])
Arguments:
- message: String, requiredNumber
- status: , default: 500Object
- context: , default: {}
Example:
`ts
import { N9Error } from '@neo9/n9-node-utils';
throw new N9Error('file-not-found', 404, { path: '/tmp/my-file.txt' });
`
Waits for the value of promise. If promise throws an Error, returns undefined.
`ts`
ok(promise: Object): Promise
Arguments:
- promise: Promise
Example:
`ts
import { readFile } from 'fs-extra';
import { ok } from '@neo9/n9-node-utils';
// readFile sends back a Promise since we use fs-extra
const file = await ok(readFile('./my-file.txt', 'utf-8'));
if (file) console.log('File found:', file);
`
Calls a function fn that takes arguments args and an (err, result) callback. Waits for the callback result, throwing an Error if err is truthy.
`ts`
cb(fn: Function, ...args: any[]): Promise
Arguments:
- fn: Function, a function that takes a callbackany
- args: (...) arguments to pass to fn
Example:
`ts
import { cb } from '@neo9/n9-node-utils';
const file = await cb(readFile('./my-file.txt', 'utf-8'));
console.log('File content:', file);
`
Waits for ms milliseconds to pass, use setTimeout under the hood.
`ts`
waitFor(ms: number): Promise
Arguments:
- ms: Number, default: 0
Example:
`ts
import { waitFor } from '@neo9/n9-node-utils';
await waitFor(1000); // wait for 1s
`
Waits for emitter to emit an eventName event.
`ts`
waitForEvent(emitter: EventEmitter, eventName: string): Promise
Arguments:
- emitter: EventEmitterString
- eventName:
Example:
`ts
import { waitForEvent } from '@neo9/n9-node-utils';
await waitForEvent(sever, 'listen');
`
Waits for all Promises in the keys of obj to resolve.
`ts`
asyncObject(obj: Object): Promise
Arguments:
- obj: Object, default: {}
Example:
`ts
import { asyncObject } from '@neo9/n9-node-utils';
const results = await asyncObject({
pictures: getPictures(),
comments: getComments(),
tweets: getTweets(),
});
console.log(results.pictures, results.comments, results.tweets);
``