Callback-oriented asynchrony, CommonJS Promise/A, Promises in Q, Promises in jQuery George Georgiev Telerik Software Academy academy.telerik.com Technical.

Slides:



Advertisements
Similar presentations
What is HTML5…?. ”…removes the need for plugins” ”…can handle multimedia directly” ”…enables rich, interactive clients” ”…enables advanced visual designs”
Advertisements

George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Telerik Software Academy HTML Basics.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
About me I attend the Edina chapter of TechMasters Been a programmer since age 13, sparked by playing video games Currently employed at GMAC as an independent.
JQuery CS 268. What is jQuery? From their web site:
NextGen Technology upgrade – Synerizip - Sandeep Kamble.
1 Web Based Programming Section 6 James King 12 August 2003.
JavaScript & jQuery the missing manual Chapter 11
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
Description, Classes, Interfaces, Hierarchy, Specifics George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Code Correctness, Readability, Maintainability Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Unleash the Power of jQuery Doncho Minkov Telerik Software Academy academy.telerik.com Senior Technical Trainer
Object Oriented Software Development
JavaScript Best Practices Learn How to Write Better Quality JavaScript Software University Technical Trainers SoftUni Team.
Templating, Routing, lodash Extending functionality using Collections SoftUni Team Technical Trainers Software University
Vitaliy Tarnavskyy Jun 26, 2013 Using Web workers in Javascript.
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Asynchronous Javascript And XML AJAX : an introduction UFCEUS-20-2 : Web Programming.
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
JS Course Program, Evaluation, Exams, Resources Telerik Software Academy JavaScript Fundamentals.
Web Technologies Lecture 7 Synchronous vs. asynchronous.
JQuery Overview Unleash the Power of jQuery SoftUni Team Technical Trainers Software University
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Unit Testing in JavaScript with Mocha, Chai and Karma SoftUni Team Technical Trainers Software University
Intro to jQuery. What is jQuery? A JavaScript library Lightweight (about 31KB for the minified version) Simplifies HTML document traversing (DOM), event.
Course Program, Evaluation, Exams George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Web Technologies Lecture 8 JQuery. “A fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax.
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
JavaScript Applications Course Introduction SoftUni Team Technical Trainers Software University
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
Promises and Asynchronous Programming Callback-oriented asynchrony, CommonJS Promise/A, Promises in Q, Promises in jQuery SoftUni Team Technical Trainers.
Course Program, Evaluation, Exams Doncho Minkov Telerik Software Academy academy.telerik.com Senior Technical Trainer
JavaScript Best Practices Learn How to Write Better Quality JavaScript Software University Technical Trainers SoftUni Team.
Course Program, Evaluation, Exams George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
JS Course Program, Evaluation, Exams, Resources Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer
C++ Exceptions.
Mocking tools for easier unit testing
JavaScript: Good Practices
Unleash the Power of jQuery
JavaScript OOP: Course Introduction
Built-in and Custom Services
Using ASP.NET Master Pages
JavaScript Applications: Course Introduction
Google Web Toolkit Tutorial
JavaScript Applications: Course Introduction
Introduction to Redux Header Eric W. Greene Microsoft Virtual Academy
Apache Cordova Overview
Promises and Asynchronous Programming
Introduction to JavaScript
A second look at JavaScript
CSE 154 Lecture 22: AJAX.
AJAX Robin Burke ECT 360.
Getting Started With the JavaScript API
jQuery The Easy JavaScript Nikolay Chochev Technical Trainer
MIS JavaScript and API Workshop (Part 3)
CS5220 Advanced Topics in Web Programming More Node.js
Lecture 12: The Fetch Api and AJAx
Introduction to JavaScript
Lecture 12: The Fetch Api and AJAx
Ajax and JSON Jeremy Shafer Department of MIS Fox School of Business
Ajax and JSON Jeremy Shafer Department of MIS Fox School of Business
CS5220 Advanced Topics in Web Programming More Node.js
Promises.
Presentation transcript:

Callback-oriented asynchrony, CommonJS Promise/A, Promises in Q, Promises in jQuery George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer

 Asynchrony in JavaScript  Callback-oriented programming  Simple callbacks  "Passing values" in callbacks  Example: Geolocation  Promises  Overview  CommonJS Promise/A and A+  Using the Q Promise Library  Promises in jQuery 2

How to do it

 JavaScript is single-threaded  Long-running operations block other operations  Asynchronous operations in JavaScript  Break up long operations into shorter ones  So other operations can "squeeze in"  Delayed execution  Postpone heavy operations to the end of the event loop  To give event handlers the ability to respond 4

 Browsers provide some asynchronous APIs  Web workers  AJAX  Geolocation  CSS3 animations, etc.  All of the above require callbacks  Functions to call at some point  When beginning to do work  After the work is done to transmit values 5

with JavaScript

 Callback function  A function object passed to another function  The other function can call the passed one  The other function can give arguments  Examples of callbacks:  Event handlers are sort-of callbacks  setTimeout and setInterval take a callback argument  Some OOP patterns use callbacks for _super 7

Live Demo

 Callback-oriented programming  Functions get passed to each other  Each functions calls the passed ones  To continue the work  To process values  Inversion of control principle ("don't call us, we'll call you")  Problems:  "Return" values by passing to other functions  Heavily nested functions are hard to understand  Errors and exceptions are a nightmare to process 9

Live Demo

How to access browser APIs asynchronously

 How do asynchronous browser APIs work?  JavaScript runs in one thread of the browser  The browser can create other threads  For its own needs, including async APIs  How do we use asynchronous APIs with JS?  Request some browser API  Pass arguments for what you want  Provide callback methods to execute when the API has processed your request 12

 Using the Geolocation API  Locating the device takes time  To request the current position  Call navigator.geolocation.getCurrentPosition  Pass in a success and error handler  i.e. pass in callback functions  Process the data  Visualize it accordingly 13

Live Demo

 We need some function nesting  We want to have good function cohesion  Provide separate functions for different operations  What will happen with a larger application?  Lots of levels of nesting  Nightmarish error-handling  Errors are easy to get lost  Handling needs to happen in inappropriate places 15

The evolution of Callback-oriented programming (switch on your imagination)

 A promise is an object which represents an eventual (future) value  Methods "promise" they will return a value  Correct or representing an error  A Promises can be in one of three states:  Fulfilled (resolved, succeded)  Rejected (an error happened)  Pending (unfulfilled yet, still being computed) 17

 Promise objects can be used in code as if their value is known  Actually we attach code which executes  When the promise is fulfilled  When the promise is rejected  When the promise reports progress (optionally)  Promises are a pattern  No defined implementation, but strict requirements  Initially described in CommonJS Promises/A CommonJS Promises/ACommonJS Promises/A 18

 More specifically:  Each promise has a.then() method accepting 3 parameters:  Success, Error and Progress function  All parameters are optional  So we can write:  * Provided promiseMeSomething returns a promise 19 promiseMeSomething().then(function (value) { //handle success here }, function (reason) { //handle error here });

 Each.then() method returns a promise in turn  Meaning promises can be chained:  Promises enable us to:  Remove the callback functions from the parameters and attach them to the "result"  Make a sequence of operations happen  Catch errors when we can process them 20 asyncComputeTheAnswerToEverything().then(addTwo).then(printResult, onError);

 The full and modern description of Promises:  CommonJS Promises/A+  An improvement of the Promises/A description  Better explanation of border cases  Several libraries fulfill the A+ spec:  A notable example is Kris Kowal's Q library

A rich CommonJS Promises/A+ library

 Some ways of using the Q module:  Option 1: Download it from the Q repository 1.Add the q.js or q.min.js file to your project 2.Reference it with a tag 3.The Q library will create a global Q object you can use  Option 2: Using NuGet in Visual Studio 1.Open the Package Manager Console 2.Type 3.Go to step 2 in the previous option 23 Install-Package Q

Live Demo

 Creating Promises with Q  We can make a regular function into a Promise  i.e. we take the return value as the value of the function  Using the function  First parameter is the function to call  The following parameters are passed into the called function  The return value of.fcall() is a promise with the function's return value 25 Q.fcall()

Live Demo

 Promises from callback-based functions  Often we need to wrap a callback in a promise  We can use the Deferred object in Q  Deferred is an object which can  self-fulfill itself with some argument:  Or self-reject itself with an error:  Get the promise which will be fulfilled/rejected: 27 var deferred = Q.defer() deferred.resolve(result) deferred.reject(reason) deferred.promise

Live Demo

 The.then() method in the Q library  Follows the specification  Success, error and progress handlers  Value returned from the promise is passed to the success handler  Errors in the promise are passed to the error handler  Any progress data the promise reports is passed to the progress handler 29

Live Demo

 Chaining promises  Each.then() method returns a new promise  The value of the promise is:  The return value of the success handler, if the previous promise is fulfilled  The error data if the previous promise failed 31

Live Demo

 Error propagation  Errors are propagated up the promise chain  The first error handler processes the error  All promises after the error are in the rejected state  No success handler will be called .done() function  Good practice to place at the end of chain  If no error handler is triggered, done will throw an exception 33

Live Demo

 Use to get a promise for a collection of promises:  Fulfilled when all promises are fulfilled  Success handler gets the results as an array  Rejected if any promise is rejected  Error handler gets the error of the first rejected  Use instead of to spread the array of results into of success handler 35 Q.all([promise1, promise2, …).spread().then() arguments

Live Demo

 We now know the basics of the Q library and Promises  There's a lot more functionality in Q  E.g. each promise instance method has a 'static' counterpart:  and  Read the documentation documentation  We will re-write the Geolocation example  Without callbacks  With promises and promise chains 37 promise.then(…) Q.then(promise, …)

Live Demo

Creation, Usage, Specifics

 jQuery supports CommonJS Promises/A  Since jQuery 1.5  * almost (details here) here .then() didn't return a promise until jQuery 1.8 .pipe() was used  Errors in handlers don't propagate up  Generally, jQuery promises look and feel the same as Q promises  Use them the same way, but be cautious 40

 The jQuery.Deferred object  Extended, mutable promise  Just like in Q  Can resolve and reject itself with arguments  Can retrieve an immutable promise object  Which in fact will be resolved/rejected 41 var d = jQuery.Deferred(); //$.Deferred() d.resolve(result); //resolves the deferred, calling success handlers d.reject(reason); // rejects the deferred, calling error handlers d.promise(); //note: here promise is a function

Live Demo

 Specifics of error propagation in jQuery  Calling reject (from deferred) works as expected  Only error handlers are called  Errors in success/error handlers are not propagated  Thrown exceptions will not be processed by error handlers in the chain 43 promiseMeSomething().then(function(){ invalid code //throws an exception }).then(function(){}, function(err){ //this error handler will not be called })

Questions?

 "Web Design with HTML 5, CSS 3 and JavaScript" Telerik Academy  html5course.telerik.com html5course.telerik.com  Telerik Software Academy  academy.telerik.com academy.telerik.com  Telerik Facebook  facebook.com/TelerikAcademy facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com forums.academy.telerik.com