![](/img/trans.png)
[英]Ionic 2 - (window as any).resolveLocalFileSystemURL() throwing error
[英]Is Ionic Framework resolveLocalFileSystemURL Asynchronous?
我從我的AngularJS控制器調用一個函數。 當函數完成執行時,我想在函數調用后打印結果。
此代碼位於控制器內部,稱為on-tap
//count the number of files inside a directory
var count = countDir(cordova.file.dataDirectory +'/Images/');
console.log('Hello ');
console.log('Count of directory '+count);
這是countDir函數。 此函數查找目錄中的文件數,然后返回計數
function countDir(path){
var count = 0;
console.log('Counting the files inside '+path);
window.resolveLocalFileSystemURL(path,
function (fileSystem) {
var reader = fileSystem.createReader();
reader.readEntries(
function (entries) {
var i;
for (i=0; i<entries.length; i++) {
console.log('File name ' + entries[i].name + ' added to count');
count++;
}
},
function (err) {
console.log(err);
}
);
}, function (err) {
console.log(err);
}
);
return count;
}
我面臨的挑戰是,我的調用代碼首先打印'Hello'和'Count of directory 0',然后打印countDir中的所有內容。
對countDir()
的調用是否異步? 如果是這樣,一旦countDir()
返回結果,我如何確保我的調用代碼繼續進行。
是的,.resolveLocalFileSystemUrl是異步的; 你可以使用$ q承諾解決你的問題:
function countDir(path){
var deff = $q.defer();
var count = 0;
console.log('Counting the files inside '+path);
window.resolveLocalFileSystemURL(path,
function (fileSystem) {
var reader = fileSystem.createReader();
reader.readEntries(
function (entries) {
var i;
for (i=0; i<entries.length; i++) {
console.log('File name ' + entries[i].name + ' added to count');
count++;
}
deff.resolve(count);
},
function (err) {
console.log(err);
deff.reject(err);
}
);
}, function (err) {
console.log(err);
deff.reject(err);
}
);
return deff.promise;
}
並將其消費為:
var count =0 ;
countDir(cordova.file.dataDirectory +'/Images/')
.then( function(result){ count = result},
function(err){ //handle error }
);
提供給window.resolveLocalFileSystemURL
和reader.readEntries
的匿名函數是異步調用的。 處理此問題的“AngularJS方法”是使用$q
服務來創建並返回一個promise。
function countDirPromise(path){
//create $q.defer object
var q = $q.defer();
var count = 0;
console.log('Counting the files inside '+path);
$window.resolveLocalFileSystemURL(path,
function onUrlSuccess(fileSystem) {
var reader = fileSystem.createReader();
reader.readEntries(
function onReaderSuccess(entries) {
var i;
for (i=0; i<entries.length; i++) {
console.log('File name ' + entries[i].name + ' added to count');
count++;
}
//resolve with count
q.resolve(count);
},
function onReaderError(err) {
console.log(err);
//reject with error
q.reject(err);
}
);
}, function onUrlError(err) {
console.log(err);
//reject with error
q.reject(err);
}
);
//return count promise
return q.promise;
}
如您所見,有四個嵌套函數: onUrlSuccess
, onUrlError
, onReaderSuccess
和onReaderError
。 這些函數是異步調用的。 如果調用onUrlSuccess
和嵌套函數onReaderSuccess
則promise將使用值count
解析。 如果調用onUrlError
函數或onReaderError
函數,則會拒絕承諾錯誤。
var countPromise = countDirPromise(cordova.file.dataDirectory +'/Images/');
console.log('Hello ');
countPromise.then(function onSuccess(count) {
console.log('Count of directory '+count);
//return to chain data
return count;
}).catch(function onReject(err) {
console.log(err);
//throw to chain rejection
throw err;
}).then(anotherSuccessFn, anotherRejectFn);
提供給雙方的功能.then
和.catch
方法由異步調用$q
服務。
有關更多信息,請參閱AngularJS $q
服務API參考 - 延遲API
另一種方法是使用ngCordova $ cordovaFile API返回的$q
服務承諾。
function countDirPromise(path){
var count = 0;
console.log('Counting the files inside '+path);
var promise = $cordovaFile.checkDir(path);
var derivedPromise = promise.then(
function onSuccess(fileSystem) {
var q = $q.defer()
var reader = fileSystem.createReader();
reader.readEntries(
function onReaderSuccess(entries) {
var i;
for (i=0; i<entries.length; i++) {
console.log('File name ' + entries[i].name + ' added to count');
count++;
}
//resolve with count
q.resolve(count);
},
function onReaderError(err) {
console.log(err);
//reject with error
q.reject(err);
}
);
//return to chain promise
return q.promise;
};
});
return derivedPromise;
}
您可以將回調傳遞給countDir函數,並在for循環后調用它。
var count = countDir(cordova.file.dataDirectory +'/Images/', function(count){
console.log('Hello ');
console.log('Count of directory '+count);
});
並在功能的定義。
function countDir(path, callback){
...
for (i=0; i<entries.length; i++) {
...
}
callback(count);
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.