4

Let's say I have a "JsonEditor" module (just for the example) which has 3 functions: get(), setProperty() and save().

Here is the code (the question follows):

var fs   = require('fs')
  , q    = require('q');

var jsonEditorModule = (function() {
    return {
        get: function(jsonPath) {
            // ...
        },

        save: function(jsonObject, jsonPath) {
            var qJson = q.defer();

            var jsonContent = JSON.stringify(jsonObject, null, 2);

            fs.writeFile(jsonPath, jsonContent, function(err) {
                if(err) {
                    qJson.reject(err);
                }
                else {
                    qJson.resolve();
                }
            });

            return qJson.promise;
        },

        setProperty: function(prop, value, jsonPath) {
            var self = this;
            var qJson = q.defer();

            this.get(jsonPath)
            .then(
                function(jsonObject) {
                    // Set the property
                    jsonObject[prop] = value;

                    // Save the file
                    self.save(jsonObject, jsonPath)
                    .then(
                        function() {
                            qJson.resolve();
                        },
                        function() {
                            qJson.reject();
                        },
                    );
                }
            );

            return qJson.promise;
        },
    };
})();

module.exports = jsonEditorModule;

See the then() right after the save() in the setProperty() function ?

It looks stupid, right ?

Do I need to manually resolve() and reject() my promise ? Can't I just transfer the save() behavior to my setProperty() promise ?

Hope the question is clear enough (and not too stupid).

Thanks

Maslow
  • 1,084
  • 9
  • 22
  • Look here and see if you can rework you call structure to set up a chain http://stackoverflow.com/questions/12461589/how-do-i-do-a-callback-chain-with-q – Steve Mitcham Mar 05 '15 at 15:20

2 Answers2

2

What you want to achieve is described here: chaining, basically if a handler returns a promise (let's call it innerPromiseFromHandler) then the handlers of the .then which was defined on the previous promise will be executed when the innerPromiseFromHandler gets a resolution value:

var jsonEditorModule = (function() {
  return {
    get: function(jsonPath) {
      return Q.delay(1000).then(function () {
        document.write('get...<br/>');
        return 'get';
      });
    },

    save: function(result) {
      return Q.delay(1000).then(function () {
        document.write('save...<br/>');        
        return result + ' save';
      });
    },

    setProperty: function(prop, value, jsonPath) {
      return this.get(jsonPath)
        .then(function(result) {
          return jsonEditorModule.save(result);
        });
    }
  };
})();
      
jsonEditorModule
  .setProperty()
  .then(function (result) {    
    document.write(result + ' finish');
  })
<script src="http://cdnjs.cloudflare.com/ajax/libs/q.js/0.9.2/q.js"></script>
Mauricio Poppe
  • 4,817
  • 1
  • 22
  • 30
  • Declaring `jsonEditorModule` outside of the IIFE pretty much defeats the point of the IIFE. You should declare it inside and return it. – JLRishe Mar 05 '15 at 16:14
  • Even if this is a CommonJS module loaded via require() ? I didn't copied the end of my file which is module.exports = jsonEditorModule; – Maslow Mar 05 '15 at 17:32
  • Thanks Mauricio. That's perfectly clear (and event better, not condescending) – Maslow Mar 05 '15 at 17:37
2

It looks stupid, right? Do I need to manually resolve() and reject() my promise?

Right, and in fact this has an own name: Manually resolving/rejecting an extra promise is known as the stupid deferred antipattern.

Can't I just transfer the save() behavior to my setProperty() promise?

Yes, this is trivially possible - and it's builtin to the then method: Calling .then() returns a new promise for the return value of your callbacks, even if that value is hidden in a promise.

var jsonEditorModule = {
    get: function(jsonPath) {
        // ...
    },
    save: function(jsonObject, jsonPath) {
        return Q.ninvoke(fs, "writeFile", jsonPath, JSON.stringify(jsonObject, null, 2));
    },
    setProperty: function(prop, value, jsonPath) {
        return this.get(jsonPath).then(function(jsonObject) {
//      ^^^^^^
            // Set the property
            jsonObject[prop] = value;
            // Save the file
            return this.save(jsonObject, jsonPath);
//          ^^^^^^
        }.bind(this));
    }
};
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375