This becomes easier if you accept that options.onSuccess(rc); executed unconditionally in the second .then() , but never in the first.
So the first .then() should go through rc :
- if
rc === 0 , in response to storage.write(c) completion - or immediately if
rc !== 0 .
.then() really convenient for this, because it naturally allows you to return the value of a new promise from its done callback.
storage.provision(c).then(function(rc) { if(rc === 0) { var dfrd = $.Deferred(); storage.write(c).done(function() { dfrd.resolve(rc); }).fail(dfrd.fail); return dfrd.promise(); } else { return rc;
I am sure that there are other approaches, but this is the closest that I can come up with for your original concept.
It would be nice to create a new Deferred when rc === 0 , but this is the most realistic approach to passing to rc , avoiding the need to modify storage.write() to behave this way.
source share