Andreas Köberle
Andreas Köberle

Reputation: 111112

How to chain jquery Deferreds

Lets say I have the following Deferred setting:

var dfr = new Deferred()

dfr.done(step1)
.then(step2)
.then(step3)

is there a way to pass the result of step2 into step3.

Upvotes: 1

Views: 71

Answers (2)

Anuj Dubey
Anuj Dubey

Reputation: 347

Yes you can pass the result .lets see an example of downloading the files so that it will be clear to you..

 fun5().then(
            function (msg) {
            if(msg==='m1'){
            var dfd1 = $.Deferred(function (dfd1){
             //alert("called2");

            var remoteFile = "http://www.freegreatdesign.com/files/images/6/2921-large-apple-icon-png-3.jpg";
            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('m2');
            // Do what you want with successful file downloaded and then
            // call the method again to get the next file
            //downloadFile();
            }, fail);
            }, fail);
            }, fail);

            //alert("In 2");
            });

            }
            return dfd1.promise();
            }).then(
            function (msg) {
            if(msg==='m2'){
            var dfd2 = $.Deferred(function (dfd2){
             //alert("called3");

            var remoteFile = "http://www.freegreatdesign.com/files/images/6/2921-large-apple-icon-png-3.jpg";
            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) {
            dfd2.resolve('m3');
            // Do what you want with successful file downloaded and then
            // call the method again to get the next file
            //downloadFile();
            }, fail);
            }, fail);
            }, fail);

            //alert("In 3");
            });

            }
            return dfd2.promise();
            })

Upvotes: 1

Beetroot-Beetroot
Beetroot-Beetroot

Reputation: 18078

Yes.

Assuming step1, step2 and step3 to be javascript functions, then simply get step2 to return its result and it automatically becomes a parameter passed to step3.

Upvotes: 1

Related Questions