Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

dojo/promise at London Ajax User Group

dojo/promise at London Ajax User Group

Details on the new dojo/promise API in Dojo 1.8.

Mark Wubben

July 10, 2012
Tweet

More Decks by Mark Wubben

Other Decks in Programming

Transcript

  1. What’s new in 1.8 dojo/promise A talk given at London

    Ajax User Group on July 10th, 2012.
  2. Brand new deferred/ Promise code Better tools for async programming

    Foundation for 2.0 Deprecated deferreds still around
  3. What’s old is new again This is for those of

    you who used Deferreds before.
  4. Promises Are objects that are returned instead of the actual

    result Promise a result in the future Can be passed to other functions Of course promises are in jQuery as well, though they behave slightly differently.
  5. function doAsync(){ … return promise; } doAsync().then(function(result){ … }); Here

    doAsync returns a promise, and we hook up the callback later.
  6. var promise = doAsync(); useAsyncResult(promise); But because the promise encapsulates

    the async operation, we can pass it to another function as well. You can’t do this with callbacks.
  7. function doAsync(){ var deferred = new Deferred(); … return deferred.promise;

    } So let's say we have a function that is asynchronous and should return a promise. Construct the deferred at the top of the function, and return the promise at the end.
  8. // Resolve a deferred if the async operation // completed

    successfully. deferred.resolve(result);
  9. // Construct a promise chain promise.then(function(result){ … }).then(function(result){ … });

    then() itself also returns a promise, to which we can add another callback.
  10. // Results & errors bubble even if no // callbacks

    are registered promise.then(function(){ … }).then(null, function(error){ … }); deferred.reject(error);
  11. // Thrown errors are handled by the resulting // promise

    promise.then(function(){ throw new Error(); }, function(error){ // Not called! });
  12. Progress updates are not cached by the promise. Progbacks must

    be registered or updates will be missed
  13. // Create a deferred with a canceler var deferred =

    new Deferred(function(reason){ … });
  14. // With a fulfilled deferred, resolving // is normally ignored:

    deferred.resolve("again!"); // Unless strict mode is desired: deferred.resolve("again!", true); Same for reject(), progress() and cancel(). With the old Deferred this throws an error.
  15. // When you don't know if you have a promise

    when(promiseOrValue, function(value){ … });
  16. // Used without direct callback, converts // into a dojo/promise/Promise

    when(promiseOrValue).then(function(result){ … });
  17. // With direct callback, will call right away // if

    passed a value, without catching errors // thrown by callback. Does not return a // promise. when(value, function(value){ throw new Error(); }); // Throws!
  18. // Returns promise fulfilled when the first // promise is

    resolved or rejected. Resolves // returned promise with result of first. first([promise1, promise2]) first({ p1: promise1, p2: promise2 })
  19. // Returns promise fulfilled when all // promises are resolved

    or one is rejected. // Resolves returned promise with list or // object of results. all([promise1, promise2]) all({ p1: promise1, p2: promise2 })