Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
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.
You can install the package either using NPM or using Yarn
npm install web3-core-promievent
yarn add 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.
The npm package web3-core-promievent receives a total of 319,399 weekly downloads. As such, web3-core-promievent popularity was classified as popular.
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.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.