deftjs / DeftJS

Extensions for Large-Scale Sencha Touch and Ext JS Applications
http://deftjs.org/
MIT License
285 stars 56 forks source link

Add 'finally' support to promises #100

Closed tomwidmer closed 11 years ago

tomwidmer commented 11 years ago

Currently it's awkward to write cleanup code that should be called when a promise completes, regardless of the result of the promise, and which passes on the existing result of the promise (e.g. resolved value, error or cancel reason). For example, https://github.com/kriskowal/q has a similar feature in the 'fin' function.

The workaround is to repeat the code. e.g.

var newPromise = promise.then({
  success:function(result) { 
    mycleanupfunc();
    return result;
  },
  failure:function(error) {
    mycleanupfunc();
    throw error;
  },
  cancel:function(reason) {
    mycleanupfunc();
    // any other way to propogate cancellation?
    var deferred = new Deft.promise.Deferred();
    deferred.cancel(reason);
    return deferred.getPromise();
  });

The ideal would be:

var newPromise = promise.fin(mycleanupfunc);

or a similar method name (such as cleanup). If the finally function throws, that should replace the existing promise result with that error (the same way that finally clauses generally work).

johnyanarella commented 11 years ago

Hey Tom,

The next release (v0.9) of Deft JS incorporates a number of changes to the Promises API.

There are some breaking changes (the last such changes to the Promise API anticipated on our way to v1.0):

and, to the specific concern you raised in this issue:

I believe the new Promise.always() implementation addresses your requirements:

Hope that helps! Please let us know if you encounter any issues with the new Promise API. We appreciated your previous bug reports, they helped us improve the Promise API as described above.

johnyanarella commented 11 years ago

I should note that a preview of v0.9 (including all of the Promise features mentioned above) is available in the master branch right now.

tomwidmer commented 11 years ago

Great, thanks - I should have checked the latest updates first since I was aware you've been enhancing promises recently.

johnyanarella commented 11 years ago

Not a problem. Let me know if you find any other potential gaps in the API.