Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
web3-core-promievent
Advanced tools
This package extends the EventEmitter with the Promise class to allow chaining as well as multiple final states of a function.
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.
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);
});
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 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 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.
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.
npm install web3-core-promievent
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;
};
// and run it
myFunc()
.on('done', console.log)
.then(console.log);
FAQs
This package extends the EventEmitter with the Promise class to allow chaining as well as multiple final states of a function.
We found that web3-core-promievent demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.