Exam 70-480 Advanced Javascript

The article is shortly describing some advanced HTML5 topics like Promises, Web Workers and Web Sockets. Rather than to re-writing from scratch about the details, I write the fundamentals about the topics and then give links for detail study.

Promises
Promise is a pattern implemented in Javascript which gives some advantages over Asynchronous Programming in Javascript.

When we execute asynchronous call using “XMLHttpRequest”, the execution does not follow linear path. You need not to wait for the call to complete execution, you just carry out with your next task and the asynchronous call will be executing in background. One thing you need to handle is successful completion and error handling.

function loadFile(){
	var xhr, results, url;
	url = "http://jinaldesai.net/sitemap.xml";
	xhr = new XMLHttpRequest();
	xhr.open('GET', url, true); //last argument denotes the call is asynchronous
	xhr.onload = function(e){
		if(this.status == 200) {
			results = this.responseText;
		}
	};
	xhr.onerror = function(e){
		handleError(e);
	}
	xhr.send();
}
function handleError(err){
	//handle error
}

In most of the scenarios you want to pass the result into another ajax request which introduces complexity into asynchronous call. When you write code for Nested Callbacks, the code grows clumsy. For handling error of nested callbacks, we need to check at several places to see if an error occured.

Promise pattern reduces such complexity and clumsiness. For example

var results = loadFile().then(filterFileValues, handleError).
              then(sortResultset, handleError).done(doFinalStuffHere);
printResults(results);

Promises are in one of the three states at any time: unfulfilled, resolved or rejected.

Promise pattern is also available with jQuery and Dojo Toolkit.
For more information about Promises refer following links.
Promises and future in Javascript

Asynchronous Programming in JavaScript with Promises

Promise Pipelines in Javascript
Javascript Promises

Web Workers
Web workers allows to run javascript in background, disregards of other scripts running or screen jam. So performance will not be affected. So, if you need to run some process which can run in background independently then use web worker.

With web workers you can bring threading concept to life in Javascript. Executing javascript in background with separate js file is just like running separate thread. The only thing you need to take care is the communication carried out between your current thread(calling file) and the thread you have initiated with web worker process.

Following are some nice examples of Web Workers for beginner.
W3School
Html 5 Rocks

Web Sockets
It is a technique for two way communication over TCP, just like client-server. You can implement kind of PUSH technology using web sockets. You can even develop chatting application, mail client application and many such applications where two communication involved between two processes resides on different server.

I have found nice tutorial on net tuts plus.
Net Tuts Plus Web Socket Tutorial

Conclusion
HTML5 come up with many new javascript things like Promises, Web Workers, Web Sockets, Storage APIs, Geolocation API and lot more. In this article I shortly described Promises, Web Workers and Web Sockets introduction with some useful links. Hope it will helpful to get basic idea of Promises, Web Workers and Web Sockets.

Exam 70-480 Advanced Javascript
0 votes, 0.00 avg. rating (0% score)

Leave a Reply

*


*