@tootallnate/once

WebJar for @tootallnate/once

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

tootallnate__once
Last Version

Last Version

1.1.2
Release Date

Release Date

Type

Type

jar
Description

Description

@tootallnate/once
WebJar for @tootallnate/once
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/TooTallNate/once

Download tootallnate__once

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/tootallnate__once/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>tootallnate__once</artifactId>
    <version>1.1.2</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/tootallnate__once/
implementation 'org.webjars.npm:tootallnate__once:1.1.2'
// https://jarcasting.com/artifacts/org.webjars.npm/tootallnate__once/
implementation ("org.webjars.npm:tootallnate__once:1.1.2")
'org.webjars.npm:tootallnate__once:jar:1.1.2'
<dependency org="org.webjars.npm" name="tootallnate__once" rev="1.1.2">
  <artifact name="tootallnate__once" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='tootallnate__once', version='1.1.2')
)
libraryDependencies += "org.webjars.npm" % "tootallnate__once" % "1.1.2"
[org.webjars.npm/tootallnate__once "1.1.2"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

@tootallnate/once

Creates a Promise that waits for a single event

Installation

Install with npm:

$ npm install @tootallnate/once

API

once<T>(emitter: EventEmitter, name: string): CancelablePromise<T>

Creates a promise that waits for event name name to occur and resolves the promise with the value of the first argument of the event handler function. If an error event occurs before the event specified by name, then the promise is rejected with the error argument.

The promise is cancelable, meaning that you may invoke promise.cancel() to remove the event handlers. The promise will never resolve in this case.

import once from '@tootallnate/once';
import { EventEmitter } from 'events';

const emitter = new EventEmitter();
const result = await once<string>(emitter, 'foo');
console.log(`got ${result}`);

// ... elsewhere ...
emitter.emit('foo', 'bar');
// "got bar"

once.spread<T>(emitter: EventEmitter, name: string): CancelablePromise<T>

Similar to the main once() function, except this version is for the less common scenario of there being more than one parameter provided to the event handler (for example, a ChildProcess "exit" event is provided with two arguments: code and signal).

When using TypeScript, the T generic type must extend Array.

import once from '@tootallnate/once';
import { spawn } from 'child_process';

const child = spawn('ls', [], { stdio: 'inherit' });

// If the process exited, `code` is the final exit code of the process, otherwise `null`.
// If the process terminated due to receipt of a signal, `signal` is the string name of the signal, otherwise `null`.
// One of the two will always be non-`null`.
const [ code, signal ] = await once.spread<[number | null, string | null]>(child, 'exit');

console.log(`child process exited with code=${code}, signal=${signal}`);

Versions

Version
1.1.2