Control asynchronous processes by multiple callbacks using Deferred

Introduction:
To control the synchronous or asynchronous flow, there may be more methods but we will do this by multiple callback mechanism using deferred and promise object.

Description:
In order to use this mechanism first we need to have an understanding of deferred and promise object.
Here we will use a promise with a deferred object.
A promise is a placeholder for a result, which is initially unknown. While a deferred represents the computation that results in the value. Every deferred has a promise, which functions as a proxy for the future result.
A promise is a value returned by an asynchronous function, but a deferred can be resolved or rejected by its caller which separates the promise from the resolver.

Now let’s see an example, where we need to download the images one by one in Android platform.

function fun(){
	// Creating a deferred object because downloading may takes some time.
	var dfd = $.Deferred(function (dfd){
   	var remoteFile = "http://YOUR LINK TO DOWNLOAD THE FILE";
	var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1);
	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,     function(fileSystem) {
		fileSystem.root.getFile(localFileName, {create: true, exclusive: false},     	
                   function(fileEntry) {
		         var localPath = fileEntry.fullPath;
		         if (device.platform === "Android" && localPath.indexOf("file://") === 0) {
				localPath = localPath.substring(7);
			 }				
		         var ft = new FileTransfer();
		         ft.download(remoteFile, localPath, function(entry) {
                              dfd.resolve('FILE DOWNLODED');// Resolve the deferred object after completion of download.
			      // Do what you want with successful file downloaded and then
			     // call the method again to get the next file
			    //downloadFile();
		         }, fail);
		    }, fail);
	}, fail);
	});
	return dfd.promise();// Return a promise that this will give the results later.
	}

	fun().then(
		function (msg) {
			if(msg==='FILE DOWNLOADED'){
			var dfd1 = $.Deferred(function (dfd1){
		var remoteFile = "http://LINKS TO DOWNLOAD THE FILES";
		var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1);
		window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
		fileSystem.root.getFile(localFileName, {create: true, exclusive: false},   					                 
                function(fileEntry) {
			var localPath = fileEntry.fullPath;
		        if (device.platform === "Android" && localPath.indexOf("file://") === 0) {
				localPath = localPath.substring(7);
			}				
				var ft = new FileTransfer();
				ft.download(remoteFile, localPath, function(entry) {
				dfd1.resolve('Second FILE DOWNLOADED ');
				// Do what you want with successful file downloaded and then
				// call the method again to get the next file
				//downloadFile();
				}, fail);
				}, fail);
				}, fail);
				
				});
				
				}
				return dfd1.promise();
				}).then(function(msg){
				if(msg==="Second FILE DOWNLOADED")
				{
					alert(“Both file downloaded”)
				}
				else
				{
				alert("Downloading Error")
				}
				});
		function fail(){
		 alert("Error occurred.");
 		}

Summary:
Here we got to know, how to control the asynchronous process using deferred and promise object.

Written By: Anuj Dubey, Software Developer, Mindfire Solutions

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s