1

假设我有以下延迟设置:

var dfr = new Deferred()

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

有没有办法将step2into的结果传递给step3.

4

2 回答 2

1

是的。

假设step1,step2step3是 javascript 函数,然后简单step2地返回它的结果,它会自动成为传递给step3.

于 2013-06-26T09:48:36.067 回答
1

是的,您可以传递结果。让我们看一个下载文件的示例,这样您就清楚了..

 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();
            })
于 2014-03-10T07:22:10.170 回答