What is web3-core-promievent?
The web3-core-promievent package is a part of the Web3.js library, which is used to interact with the Ethereum blockchain. This specific package provides a hybrid of Promise and EventEmitter, allowing developers to handle asynchronous operations with both promises and events.
What are web3-core-promievent's main functionalities?
Creating a PromiEvent
This code demonstrates how to create a PromiEvent that emits an event and resolves a promise after an asynchronous operation.
const PromiEvent = require('web3-core-promievent');
function asyncOperation() {
const promiEvent = new PromiEvent();
setTimeout(() => {
promiEvent.eventEmitter.emit('done', 'Operation completed');
promiEvent.resolve('Success');
}, 1000);
return promiEvent.eventEmitter;
}
asyncOperation().on('done', (message) => {
console.log(message);
}).then((result) => {
console.log(result);
});
Handling Errors
This code demonstrates how to handle errors in a PromiEvent by emitting an error event and rejecting the promise.
const PromiEvent = require('web3-core-promievent');
function asyncOperationWithError() {
const promiEvent = new PromiEvent();
setTimeout(() => {
promiEvent.eventEmitter.emit('error', new Error('Operation failed'));
promiEvent.reject(new Error('Failure'));
}, 1000);
return promiEvent.eventEmitter;
}
asyncOperationWithError().on('error', (error) => {
console.error(error.message);
}).catch((error) => {
console.error(error.message);
});
Listening to Multiple Events
This code demonstrates how to listen to multiple events emitted by a PromiEvent during an asynchronous operation.
const PromiEvent = require('web3-core-promievent');
function asyncOperationWithMultipleEvents() {
const promiEvent = new PromiEvent();
setTimeout(() => {
promiEvent.eventEmitter.emit('step', 'Step 1 completed');
promiEvent.eventEmitter.emit('step', 'Step 2 completed');
promiEvent.resolve('All steps completed');
}, 1000);
return promiEvent.eventEmitter;
}
asyncOperationWithMultipleEvents().on('step', (message) => {
console.log(message);
}).then((result) => {
console.log(result);
});
Other packages similar to web3-core-promievent
bluebird
Bluebird is a fully featured promise library with focus on innovative features and performance. It provides a wide range of utilities for working with promises, including event handling, which makes it somewhat comparable to web3-core-promievent.
eventemitter3
EventEmitter3 is a high performance EventEmitter that is used for handling events in JavaScript. While it does not provide promise functionality out of the box, it can be combined with promises to achieve similar results to web3-core-promievent.
rxjs
RxJS is a library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code. It provides powerful event handling and asynchronous data stream capabilities, which can be used similarly to web3-core-promievent.
web3-core-promievent
This is a sub-package of web3.js.
This is the PromiEvent package used to return a EventEmitter mixed with a Promise to allow multiple final states as well as chaining.
Please read the documentation for more.
Installation
Node.js
npm install web3-core-promievent
Usage
const Web3PromiEvent = require('web3-core-promievent');
const myFunc = function(){
const promiEvent = Web3PromiEvent();
setTimeout(function() {
promiEvent.eventEmitter.emit('done', 'Hello!');
promiEvent.resolve('Hello!');
}, 10);
return promiEvent.eventEmitter;
};
myFunc()
.on('done', console.log)
.then(console.log);