1.0.0 • Published 8 years ago

promise-fallback v1.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
8 years ago

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 $@