1 / 24

Promises, promise...

Promises, promise. @ GarrenSmith. “Promises are a software abstraction that makes working with asynchronous operations much more pleasant.” @Dominic http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/. Promises/A+. https:// github.com /promises- aplus /promises-spec.

scott
Télécharger la présentation

Promises, promise...

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Promises, promise... @GarrenSmith

  2. “Promises are a software abstraction that makes working with asynchronous operations much more pleasant.” @Dominic http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/

  3. Promises/A+ https://github.com/promises-aplus/promises-spec

  4. Async is not synchronous! var ping =null; $.get('/ping',function(result){   ping = result; }); console.log(ping) // null;

  5. Promises // jqXHR var promise =backboneModel.save(); promise.then(function(resp){ // do something with saved model }); promise.fail(function(err){ // handle failed error });

  6. Promises // jqXHR var promise =backboneModel.save(); varfinalPromise=promise.then(function(resp){ var promise2 =anotherAsyncMethod(); return promise2; }); finalPromise.then(function(result){ // called once promise 2 completed });

  7. Promises vs. Events myBlogPost.on(‘save’,function(model){ // do something with saved model });

  8. Promises vs. Events // Somewhere else before event declaration myBlogPost.save(); myBlogPost.on(‘save’,function(model){ // do something with saved model });

  9. Promises vs. Events // Somewhere else before event declaration savePromise=myBlogPost.save(); savePromise.done(function(model){ // do something with save promise });

  10. Promises vs. Callbacks doFancyCalculation( input,function(err, result){ if(err){ returnconsole.log('oh no! an error', err); } parseCalulatedResult( result,function(err,parsedResult){ if(err){ returnconsole.log('damn it failed', err); } saveResult(parsedResult,function(err,savedResult){ if(err){ returnconsole.log('could not save', err); } alert('yay, saved'); }); }); });

  11. Promises vs Callbacks doFancyCalculation(input)   .then(parseCalulatedResult,handleErr)   .then(saveCalculatedResult,handleErr);

  12. Further promise api var promise =myBlog.save(); promise.done(function(){ // called when promise has been resolved }); promise.fail(function(){ // called if promise fails/rejected }); promise.always(function(){ // called when either rejected/resolved });

  13. Further promise api uploadFile() .then(function(){ // Success uploading the file },function(err){ // An error occurred with a reason for error },function(progress){ // Notify of the upload's progress as it is executed });

  14. Serial and Parallel doFancyCalculation(input)   .then(parseCalulatedResult,handleErr)   .then(saveCalculatedResult,handleErr);

  15. Serial and Parallel var promise1 = asyncOperation1(),     promise2 = asyncOperation2(),     promise3 = asyncOperation3(); varfinalPromise= $.when(promise1, promise2, promise3); finalPromise.done(function(){ console.log('all async operations finished'); });

  16. Serial and Parallel // unknown number of promises varpromises =comments.map(function(comment){ returncomment.save(); }); $.when.apply(null,promises);

  17. Creating Promises functionasyncMethod(value){ var promise = $.Deferred(); setTimeout(function(){ if(value >100){ promise.resolve(value); }else{ promise.reject('Value is too low.'); } },1000); return promise; };

  18. PipePromises Mixins.SaveQueue={   save:function(attrs,options){ var self =this, save = Backbone.Model.prototype.save; this._queue.then(function(){ returnsave.call(self,attrs, options); }); returnthis._queue; } };

  19. Power of Q https://github.com/kriskowal/q

  20. Interfacing with node.js fs.readFile('/etc/passwd',function(err, data){ if(err)throw err; console.log(data); });

  21. Interfacing with node.js varreadFile=Q.denodeify(FS.readFile); var promise =readFile("/etc/passwd"); promise.then(function(data){ console.log(data) },function(err){ console.log('ERR', err); });

  22. Why Promises are awesome • Cleaner method signatures • Easy composition • Easy to handle sequential and parallel flow • Always asynchronous • Uncomplicated error handling

  23. Thanks for listening garren@redcometlabs.com Wanna learn more? Speak to me about Javascript training.

More Related