Creates an AsyncQueue that will receive events. The value of each queue item is the value associated with the event, or an array if there were multiple values.
const receivedErrors = eventSink(client, "error");
const firstError = await receivedErrors.take();
const secondError = await receivedErrors.take();
An event emitter.
The event name.
A Promise that is resolved the first time the event is raised. It will contain the value that was provided with the event, if any-- or, if there were multiple values, an array of them.
Causes a rejection if the given promise resolves before the timeout elapses.
If the original promise resolves within the timeout, the new promise rejects with an UnexpectedResolveError. If the original promise rejects within the timeout, the new promise rejects with the same result. Otherwise, the new promise resolves with no value.
any promise
how long to wait
optional message that will be included in the UnexpectedResolveError
a promise that resolves only if the original promise neither resolved nor rejected
Causes a rejection if the timeout elapses before the given promise resolves.
If the original promise resolves or rejects within the timeout, the new promise resolves or rejects with the same result. If the timeout elapses first, the new promise rejects with a TimeoutError.
Test frameworks such as Jest also allow you to set an overall timeout for a test. The difference between doing that and using failOnTimeout is that failOnTimeout allows you to be more specific about which step in the test has the timing requirement, and also allows you to customize the failure message to provide better clues to what failed (since the stacktrace may not be useful in async code).
any promise
how long to wait
optional message that will be included in the TimeoutError
a promise that behaves the same as the original promise unless a timeout occurs
Converts a function whose last parameter is a Node-style callback (err, result)
into a function
with one fewer argument that returns a Promise.
This is equivalent to util.promisify
, but since Node 6 does not provide promisify
, it is
reimplemented here for projects that need to support Node 6. Note that it currently does not
type-check the arguments.
function someFunction(param1, param2, callback) { callback(err, result); }
var result = await promisify(someFunction)(param1, param2);
An asynchronously implemented function whose last parameter is an error-and-value callback.
An equivalent function that returns a Promise. The Promise will be resolved if the original
function calls its callback with no error (null, returnValue)
, or rejected if it was called
with an error (or if the original function threw an exception).
Converts a function whose last parameter is a Node-style callback (err, result)
into a function
with one fewer argument that returns a Promise.
This is equivalent to util.promisify
, but since Node 6 does not provide promisify
, it is
reimplemented here for projects that need to support Node 6. Note that it currently does not
type-check the arguments.
function someFunction(param1, param2, callback) { callback(err, result); }
var result = await promisify(someFunction)(param1, param2);
An equivalent function that returns a Promise. The Promise will be resolved if the original
function calls its callback with no error (null, returnValue)
, or rejected if it was called
with an error (or if the original function threw an exception).
Similar to promisify, but for functions whose callback takes only a single parameter (result)
instead of the typical Node style (err, result)
.
function someFunction(param1, param2, callback) { callback(result); }
var result = await promisify(someFunction)(param1, param2);
An asynchronously implemented function whose last parameter is a single-value callback.
An equivalent function that returns a Promise. The Promise will be resolved if the original function calls its callback with a value, or rejected if the original function threw an exception.
Similar to promisify, but for functions whose callback takes only a single parameter (result)
instead of the typical Node style (err, result)
.
function someFunction(param1, param2, callback) { callback(result); }
var result = await promisify(someFunction)(param1, param2);
An equivalent function that returns a Promise. The Promise will be resolved if the original function calls its callback with a value, or rejected if the original function threw an exception.
Fully reads a stream.
The stream to read.
A Promise that is resolved with the full content.
Returns a Promise based on setTimeout()
.
await sleepAsync(5000);
The length of the delay.
A Promise that is resolved after the delay. It is never rejected.
Ensures that an object"s close() method is called after executing a callback, even if an error occurs.
await withCloseable(myExistingObject, async o => doSomething(o));
await withCloseable(() => makeNewObject(), async o => doSomething(o));
await withCloseable(async () => await makeObjectAsync(), async o => doSomething(o));
Either the object itself, or a function (sync or async) that will create it. If this is null or undefined, you will receive a rejected Promise.
An async function to execute with the object as a parameter.
A Promise that resolves with the return value of the callback.
Generated using TypeDoc
A function that provides a response for TestHttpServer. May be synchronous or async.