Socket
Socket
Sign inDemoInstall

promise-accum

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-accum

Solve variable scoping and/or callback hell in promises


Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

Installation

npm install promise-accum

Motivation

Often when using promises, variable scoping becomes an issue, specifically when doing an operation that requires something along the lines of "load two things in the database, then do something based on that".

There are a few ways to solve this problem:

  1. Stay in callback hell

    LoadItemOne().then(function(item1) {
    	return LoadItemTwo().then(function(item2) {
    		return DoSomethingWithItems(item1, item2);
    	});
    });
    
  2. Enter variable scoping hell

    var item1;
    LoadItemOne().then(function(i) {
        item1 = i;
        return LoadItemTwo();
    }).then(function(item2) {
    	return DoSomethingWithItems(item1, item2);
    });
    
  3. Return ugly objects

    LoadItemOne().then(function(item2) {
        return LoadItemTwo().then(function(item2) {
            return { item1: item1, item2: item2 }
        });
    }).then(function(items) {
    	return DoSomethingWithItems(items.item1, items.item2);
    });
    

This library provides a solution. Context is stored in a variable called $. To add the result of a promise to the context, call $('key'). In this case:

var $ = require('promise-accum');

LoadItemOne().then($('item1')).then(function($) {
    return LoadItemTwo().then($('item2'));
}).then(function($) {
	return DoSomethingWithItems($.item1, $.item2);
});

Error handling

Using this solution, the context object must be passed through the entire chain:

LoadItemOne().then($('item1')).then(function($) {
    $.item1.foo = 'bar';
}).then(function($) {
    // $ is not the context, obviously.
});

In most cases, it's as simple as returning $ in the promise. However, in some promise libraries (q and bluebird, among others), errors can be handled by calling .catch(errorHandler). This is effectively an alias for .then(null, errorHandler) – of course, in this case, the context does not get passed through, so any functions after the error handler it.

To mitigate this, promise-accum provides $.passthrough:

LoadItemOne().then($('item1')).then($.passthrough, errorHandler).then(function($) {
    // $ is maintained.
});

Keywords

FAQs

Package last updated on 15 Oct 2014

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