Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

promise-fallback

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-fallback

Library for handling empty promise resolutions

latest
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

promise-fallback

Function for promise fallback flow control Returns the first resolution that is not null or undefined

Installation

npm install promise-fallback

Require

var fallback = require('./fallback.js').fallback

#Usage

fallback(array); //returns promise

#example

function lookupDataByKey() {
  return fallback([
    localCache[key], //this could be a value like 2 or null

    cachedPromises[key], //this could be a promise that resolves to a value

    function() { //this could resolve to our data
      //this only gets called if the previous attempts resolve to null/undefined
      return getDataFromRedisAsync(key);
    },

    function() { //this could resolve to our data
      //this only gets called if the previous attempts resolve to null/undefined
      return getDataFromDatabaseAsync(key);
    },

    function() { //our data was not found throw an error
      //this only gets called if the previous attempts resolve to null/undefined
      throw new Error('Data not found with key: ' + key);
    },

    function() { //this code will NEVER execute because the previous attempt rejected
      return 22;
    }
  ]);  
}

#Build

sh build.sh

or

coffee -p --no-header -c src/fallback.coffee > fallback.js

#Test

sh test.sh

or

mocha --compilers coffee:coffee-script/register -R spec --timeout 10000 $@

FAQs

Package last updated on 30 May 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