eventful-promise
Create a Promise/EventEmitter hybrid that can emit events even after being resolved
Last updated 2 years ago by danielkalen .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install eventful-promise 
SYNC missed versions from official npm registry.

eventful-promise

Build Status Coverage Code Climate NPM

Create a Promise/EventEmitter hybrid that can emit events even after being resolved.

Installation

npm install --save eventful-promise

Usage

var EventfulPromise = require('eventful-promise');
var myPromise = new EventfulPromise(function(resolve, reject){
    this.emit('some-event');
    
    setTimeout(()=> {
        this.emit('some-event');
        this.emit('another-event');
    }, 50);
    
    setTimeout(()=> {
        this.emit('about-to-resovle');
        resolve(123456);
    }, 100)
    
    setTimeout(()=> {
        this.emit('some-event');
        this.emit('another-event');
        this.emit('post-resolution');
    }, 200)
});
myPromise
    .on('some-event', ()=> console.log('A'))
    .once('another-event', ()=> console.log('B'))
    .on('about-to-resovle', ()=> console.log('C'))
    .on('post-resolution', ()=> console.log('D'))
    .then((result)=> console.log('E', result))
    .catch((err)=> console.error(err))

Output of above code:

console -> A
console -> A
console -> B
console -> C
console -> E 123456
console -> A
console -> D

API

Creation

There are three ways you can create an eventful promise:

Method Example
Callback new EventfulPromise((resolve, reject)=> )
Thenable new EventfulPromise(Promise.resolve('value'))
.resolve() EventfulPromise.resolve('value').then(...)

Emitting events

EventfulPromise instances inherit from EventEmitter and thus have methods such as .emit() and .on and .once

var promise = EventfulPromise.resolve();
promise.emit('someEvent');
promise.emit('anotherEvent');
promise.on('someEvent', ...);
promise.once('anotherEvent', ...);

The callback provided to the EventfulPromise constructor and the callback provided to the eventfulPromise.then() method will be invoked with the EventfulPromise instance as their context, thus allowing the instance to be referenced via the this keyword inside those callbacks.

var promise = new EventfulPromise(function(resolve){
    this.emit('someEvent');
    resolve();
});

promise
    .then(function(){ this.emit('someEvent') });

EventfulPromise.resolve()
    .then(function(){ this.emit('someEvent') });

License (MIT) Copyright 2017 danielkalen

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.

Current Tags

  • 2.0.1                                ...           latest (2 years ago)

4 Versions

  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 0
Last Month 1
Dependencies (3)
Dev Dependencies (5)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |