Code Monkey home page Code Monkey logo

p-event's Introduction

p-event Build Status

Promisify an event by waiting for it to be emitted

Useful when you need only one event emission and want to use it with promises or await it in an async function.

Install

$ npm install --save p-event

Usage

const pEvent = require('p-event');
const emitter = require('./some-event-emitter');

pEvent(emitter, 'finish')
	// Called when `emitter` emits a `finish` event
	.then(result => {
		console.log(result);
	})
	// Called when `emitter` emits an `error` event
	.catch(error => {
		console.error(error);
	});
const pEvent = require('p-event');

pEvent(document, 'DOMContentLoaded').then(() => {
	console.log('😎');
});

API

pEvent(emitter, event, [options])

Returns a Promise that is fulfilled when emitter emits an event matching event, or rejects if emitter emits any of the events defined in the rejectionEvents option.

The returned promise has a .cancel() method, which when called, removes the event listeners and causes the promise to never be settled.

emitter

Type: Object

Event emitter object.

Should have either a .on()/.addListener()/.addEventListener() and .off()/.removeListener()/.removeEventListener() method, like the Node.js EventEmitter and DOM events.

event

Type: string

Name of the event to listen to.

If the same event is defined both here and in rejectionEvents, this one takes priority.

options

Type: Object

rejectionEvents

Type: Array
Default: ['error']

Events that will reject the promise.

Before and after

const fs = require('fs');

function getOpenReadStream(file, callback) {
	const stream = fs.createReadStream(file);

	stream.on('open', () => {
		callback(null, stream);
	});

	stream.on('error', error => {
		callback(error);
	});
}

getOpenReadStream('unicorn.txt', (error, stream) => {
	if (error) {
		console.error(error);
		return;
	}

	console.log('File descriptor:', stream.fd);
	stream.pipe(process.stdout);
});
const fs = require('fs');
const pEvent = require('p-event');

async function getOpenReadStream(file) {
	const stream = fs.createReadStream(file);
	await pEvent(stream, 'open');
	return stream;
}

getOpenReadStream('unicorn.txt')
	.then(stream => {
		console.log('File descriptor:', stream.fd);
		stream.pipe(process.stdout);
	})
	.catch(console.error);

License

MIT Β© Sindre Sorhus

p-event's People

Contributors

sindresorhus avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.