Coder Social home page Coder Social logo

ruiaraujo / deferredwithupdate.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bennadel/deferredwithupdate.js

0.0 2.0 0.0 104 KB

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: Other

HTML 59.93% JavaScript 40.07%

deferredwithupdate.js's Introduction

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.

deferredwithupdate.js's People

Contributors

bennadel avatar

Watchers

Rui Araújo avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.