次の Deferred 設定があるとします。
var dfr = new Deferred()
dfr.done(step1)
.then(step2)
.then(step3)
step2
の結果をに渡す方法はありますかstep3
。
次の Deferred 設定があるとします。
var dfr = new Deferred()
dfr.done(step1)
.then(step2)
.then(step3)
step2
の結果をに渡す方法はありますかstep3
。
はい。
が であり、javascript 関数であると仮定するとstep1
、 getはその結果を返すだけで、自動的に に渡されるパラメータになります。step2
step3
step2
step3
はい、結果を渡すことができます。ファイルをダウンロードする例を見てみましょう。
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();
})