Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

web3-core-promievent

Package Overview
Dependencies
Maintainers
4
Versions
124
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

web3-core-promievent

This package extends the EventEmitter with the Promise class to allow chaining as well as multiple final states of a function.

  • 1.10.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
366K
decreased by-5.97%
Maintainers
4
Weekly downloads
 
Created

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

FAQs

Package last updated on 18 Oct 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc