combine promise and eventemitter

Solutions on MaxInterview for combine promise and eventemitter by the best coders in the world

showing results for - "combine promise and eventemitter"
Eleonora
26 Jun 2019
1// you can use default eventemitter or using third part library, checkout about eventemitter3 in npm
2const { EventEmitter } = require('eventemitter3')
3const reusify = require('reusify')
4
5/**
6 * @description Create owner custom costructor for inhertitance event emitter data
7 */
8function EventDriven() {
9	/**
10	 * @description Inheritance all event core method from EventEmitter to reusify Function
11	 */
12	this.reusify = reusify(EventEmitter).get()
13	Object.defineProperties(this.reusify, {
14		nextTick: {
15			value: process.nextTick,
16			writable: true,
17			enumerable: true
18		},
19		parse: {
20			value: JSON.parse,
21			writable: true,
22			enumerable: true
23		},
24		stringify: {
25			value: JSON.stringify,
26			writable: true,
27			enumerable: true
28		},
29		promise: {
30			value: Promise,
31			writable: true,
32			enumerable: true
33		},
34		eventName: {
35			value: undefined,
36			writable: true,
37			enumerable: true
38		},
39		data: {
40			value: undefined,
41			writable: true,
42			enumerable: true
43		},
44		response: {
45			value: undefined,
46			writable: true,
47			enumerable: true
48		}
49	})
50}
51
52EventDriven.prototype.pub = function (eventName, data) {
53	this.reusify.eventName = eventName
54	this.reusify.data = data
55
56	if (this.reusify.data !== undefined) {
57		this.reusify.nextTick(() => this.reusify.emit(this.reusify.eventName, this.reusify.stringify({ data: this.reusify.data })))
58		return Promise.resolve(this.reusify)
59	}
60}
61
62EventDriven.prototype.sub = function (eventName) {
63	let self = this
64	self.reusify.response = new self.reusify.promise(function (resolve, reject) {
65		self.reusify.eventName = eventName
66
67		self.reusify.on(self.reusify.eventName, function (data) {
68			self.reusify.data = data
69			self.reusify.response = self.reusify.parse(self.reusify.data)
70			resolve(self.reusify.response.data)
71		})
72
73		if (self.reusify.response !== (undefined && null)) {
74			/**
75			 * @description after success unsubscribe event name
76			 */
77
78			self.reusify.nextTick(() => self.reusify.off(self.reusify.eventName))
79
80			/**
81			 * @description after subscribe success reset property value
82			 */
83
84			self.reusify.nextTick(() => {
85				self.reusify.eventName = undefined
86				self.reusify.data = undefined
87				self.reusify.response = undefined
88			})
89		}
90	})
91
92	return self.reusify.response
93}
94
95exports.event = new EventDriven()
Claudio
01 Apr 2016
1const { EventEmitter } = require('events');
2const fs = require('fs');
3
4function doSomething(parameters) {
5  var resolves;
6  var rejects;
7  const emitter = new EventEmitter();
8  const promise = new Promise((resolve, reject) => {
9    resolves = resolve;
10    rejects = reject;
11  });
12  promise.on = emitter.on;
13  promise.emit = emitter.emit;
14
15  // DO DIRTY JOB
16  fs.readdir(parameters.directory, (err, files) => {
17    if (err) {
18      rejects(err);
19      return;
20    }
21    files.forEach(file => promise.emit('update-event', file));
22    resolves(`I'm done: ${parameters.param} world`);
23  });
24
25  return promise;
26}
27
28const work = doSomething({ param: 'hello', directory: './' });
29work.on('update-event', data => console.log(`Update ${data}`))
30  .then(console.log)
31  .catch(console.error);