Skip to content

Instantly share code, notes, and snippets.

@joepie91
Last active August 2, 2016 21:04
Show Gist options
  • Save joepie91/f6a56acdae303e90e44a to your computer and use it in GitHub Desktop.
Save joepie91/f6a56acdae303e90e44a to your computer and use it in GitHub Desktop.

Revisions

  1. joepie91 revised this gist Aug 2, 2016. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion new-promises-fallback.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ Promise.try(function(){
    return fs.readFileAsync("./config.json").then(JSON.parse);
    }).catch({code: "ENOENT"}, function(err){
    /* Return an empty object. */
    return Promise.resolve({});
    return {};
    }).then(function(config){
    /* `config` now either contains the JSON-parsed configuration file, or an empty object if no configuration file existed. */
    });
    2 changes: 1 addition & 1 deletion old-promises-fallback.js
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ Promise.try(function(){
    return fs.readFileAsync("./config.json").then(JSON.parse);
    }).catch(NonExistentFilePredicate, function(err){
    /* Return an empty object. */
    return Promise.resolve({});
    return {};
    }).then(function(config){
    /* `config` now either contains the JSON-parsed configuration file, or an empty object if no configuration file existed. */
    });
  2. joepie91 revised this gist Oct 29, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions old-promises-fallback.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    /* This example is ONLY for Bluebird 2.x. When using Bluebird 3.0 or newer, you should
    * use the updated example above instead. */

    var Promise = require("bluebird");
    var fs = Promise.promisifyAll(require("fs"));

  3. joepie91 revised this gist Oct 29, 2015. 2 changed files with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions new-promises-fallback.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    /* UPDATED: This example has been changed to use the new object predicates, that were
    * introduced in Bluebird 3.0. If you are using Bluebird 2.x, you will need to use the
    * older example below, with the predicate function. */

    var Promise = require("bluebird");
    var fs = Promise.promisifyAll(require("fs"));

    Promise.try(function(){
    return fs.readFileAsync("./config.json").then(JSON.parse);
    }).catch({code: "ENOENT"}, function(err){
    /* Return an empty object. */
    return Promise.resolve({});
    }).then(function(config){
    /* `config` now either contains the JSON-parsed configuration file, or an empty object if no configuration file existed. */
    });
    File renamed without changes.
  4. joepie91 revised this gist May 2, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion promises-fallback.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    var Promise = require("bluebird");
    var fs = Promise.promisifyAll(require("fs"));

    var NonExistFilePredicate = function(err) {
    var NonExistentFilePredicate = function(err) {
    return (err.code === "ENOENT");
    };

  5. joepie91 created this gist Mar 12, 2015.
    15 changes: 15 additions & 0 deletions promises-fallback.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    var Promise = require("bluebird");
    var fs = Promise.promisifyAll(require("fs"));

    var NonExistFilePredicate = function(err) {
    return (err.code === "ENOENT");
    };

    Promise.try(function(){
    return fs.readFileAsync("./config.json").then(JSON.parse);
    }).catch(NonExistentFilePredicate, function(err){
    /* Return an empty object. */
    return Promise.resolve({});
    }).then(function(config){
    /* `config` now either contains the JSON-parsed configuration file, or an empty object if no configuration file existed. */
    });