Skip to content Skip to sidebar Skip to footer

Chaining Promises Without Using 'then' With Q Library

I'm trying to chain Q promises without 'then', so eventually the chain would look like this: var foo = new Foo(); foo .method1() .method2() .method3(); How to implement foo's meth

Solution 1:

I'm not sure if you are going to gain anything with this.

I suppose that you can do something like this:

functionFoo() {
  var self = this,
      lastPromise = Promise.resolve();

  var chainPromise = function(method) {
    returnfunction() {
      var args = Array.prototype.slice.call(arguments))
      lastPromise = lastPromise.then(function(){
        return method.apply(self, args);
      });
      return self;
    }
  }

  this.method1 = chainPromise(function() {
    // do the method1 stuff// return promise
  });

  this.method2 = chainPromise(function() {
    // do the method2 stuff// return promise
  });

  // etc...
}

Post a Comment for "Chaining Promises Without Using 'then' With Q Library"