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

brianmhunt-mutex-promise

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

brianmhunt-mutex-promise

Promises with uncaught handling and events

  • 1.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-77.78%
Maintainers
1
Weekly downloads
 
Created
Source
Travis Status Promises/A+ logo

Mutex Promise

A+ Compliant (and mostly follows ECMA-262) ES6 Promises with a few extra features that can help with debugging :

  • events
  • temporal mutual exclusion
  • uncaught checking
  • creation and chaining call stack

This implementation is primarily designed for testing, and a reasonably readable spec-compliant implementation. Its main intended purpose is as a drop-in replacement for the built-in ES6 implementation when one is developing and debugging

This implementation is not intended to be used in production. As it is itself written in ES6 Javascript meaning,

  • there's already a native Promise class;
  • this would need to be transpiled for backwards compatibility; and, in any case
  • this implementation is not fast compared to others.

Install

$ npm install brianmhunt-mutex-promise

Events

The following events are available:

  • new - a new promise was created
  • reject - a promise was rejected
  • resolve - a promise was resolved
  • uncaught - a rejection not caught
  • trespass - a promise was resolved/rejected outside its mutex

The this of each shall be the promise from which it was thrown.

Register events with e.g.

Promise.on('new', function () { /* this = promise instance */ })

Unregister events with

Promise.off('new', fn)

Temporal mutual exclusion

You can set an identifier that when not strictly equal when promises are chained will result in atrespass event being emitted. For example:

Promise.setMutex("abc")
p = new Promise(function () {}).then(function () {})
Promise.setMutex("def")
p.then(function () {})

// This will emit `trespass` twice - once for both `then`'s because they
// are both asynchronously resolved.

The trespass event receives a data argument with two events, like this:

{
  promiseMutexTo: this.mutexTo,
  mutexId: MutexPromise.mutexId
}

For a more solid example, consider a testing scenario with Mocha:

beforeEach(function () {
  // Any promises created after this, but before the next `beforeEach` will
  // be in the same mutex period.
  Promise.setMutex(this.currentTest)  
})

after(function () {
  // Catch any promises resolved after testing completes.
  Promise.setMutex('Tests complete')
})

Promise.on('trespass', function (data) {
  console.error("A promise started in test ", data.promiseMutexTo,
    "was concluded after that test completed, specifically:", data.mutexId)
})

Uncaught Checking

An uncaught event is raised, following roughly the logic of:

  • A rejection is raised;
  • After a short period of time, no rejection handler has been added (via .then, race or all) that would catch it

Note:

Creation and Chaining Call Stack

Each promise instance has a creationStack property, and once resolved or rejected a resolutionStack property.

License & Thanks

© 2016 Brian M Hunt (MIT License)

Thanks to NetPleadings/Conductor for time to work on this!

Keywords

FAQs

Package last updated on 14 Mar 2016

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