Skip to content
This repository has been archived by the owner on May 10, 2018. It is now read-only.
/ promisify-events Public archive

(DEFUNCT) promisify-events listens to specified events on an EventEmitter and resolves or rejects a Promise from them. Simple but effective.

License

Notifications You must be signed in to change notification settings

novacrazy/promisify-events

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

promisify-events

This project is defunct and outdated. I would not recommend using it, but feel free to browse the code and steal anything useful.

NPM Version NPM Downloads MIT License

Intro

While many solutions like this exist, this is my take on it. It provides a single clean function to listen to an EventEmitter and return a Promise instance that will be resolved or rejected based on the event names given.

Quickstart

import {createServer} from 'http';
import {promisifyEvents} from 'promisify-events';

let server = createServer(function(req, res) {
    res.end("Hello, World!");
});

promisifyEvent(server, 'listening').then(function() {
    console.log("Listening on port 8080").
});

server.listen(8080);

or something like

//...
import {promisifyEvents} from 'promisify-events';
import {createWriteStream} from 'fs';
//...

let stream = createWriteStream("save.dat");

promisiftEvents(stream, 'end', 'error').then(function() {
    console.log("Save complete.");
}).catch(function(err) {
    console.error("Error saving:", err);
});

You can even use multiple events or a combination of single and multiple events.

promisifyEvents(someEmitter, ['success', 'end', 'done'], 'error').then(function() {
    console.log("Finished");
}).catch(function(err) {
    console.error("Error: ", err);
});

Reference

promisifyEvents(emitter: EventEmitter, resolve_events?: string|array<string>, reject_events?: string|array<string>) -> Promise<any>

If any of the events produce more than one value, they will be resolved as an array, since only one value can be resolved.

Support

License

The MIT License (MIT)

Copyright (c) 2015-2016 Aaron Trent

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

(DEFUNCT) promisify-events listens to specified events on an EventEmitter and resolves or rejects a Promise from them. Simple but effective.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published