Skip to content

This is an AngularJS class which augments the core $q class with update() and mistake() methods that provide callback hooks for deferred values that need to be resolved or rejected after the fact. This is intended to be used with cached responses.

License

ruiaraujo/DeferredWithUpdate.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DeferredWithUpdate.js

by Ben Nadel www.bennadel.com

DeferredWithUpdate.js is an AngularJS class that decorates the core $q class with two additional promise methods:

  • promise.update()
  • promise.mistake()

By default, a Deferred object can only be resolved or rejected once in its life cycle. The same is (mostly) true for the DeferredWithUpdate.js. The difference is that if the resolve or reject methods are called after a Deferred instance has been taken out of its pending states, the update() and mistake() handlers will be invoked (rather than ignored).

Right now, this only decorates deferred objects that are create with the defer() method. The other $q methods (all(), reject(), when()) are simply passed-through to the underlying $q instance.

Intended To Be Used With Cached Responses

At first, this might sound like a completely crazy idea; but, it does have a very specific use case: using cached data to temporarily resolve a deferred response from the server.

When the request is initially made, the service layer could instantly resolve the deferred response with cached data (if its available). Then, once the actual data is returned, the deferred object could be "updated" with the fresh data. Similarly, if an already-resolved response returns a 404 (or some other failed HTTP status code), the deferred object could trigger the "mistake" callbacks.

The beautiful thing about this is that the API for the Deferred object is still exactly the same:

  • resolve()
  • reject()

The code that instantiates the DeferredWithUpdate.js doesn't have to change its behavior at all. The invoking of the update() and mistake() methods are handled completely internally to the DeferredWithUpdate.js class.

About

This is an AngularJS class which augments the core $q class with update() and mistake() methods that provide callback hooks for deferred values that need to be resolved or rejected after the fact. This is intended to be used with cached responses.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 59.9%
  • JavaScript 40.1%