我正在尝试获取联系人列表详细信息,并尝试使用内部逻辑对其进行评估。但是我能够在任何时候只获得一个变量,而另一个变量未定义。我知道这是由于异步功能,但不知道如何实现自定义方法的订阅/承诺,因为我无法分配订阅它。请参阅下面的代码。
getCallerAudio() {
this.file.listDir("file:///storage/emulated/0/", 'myfolder').then((result) => {
for (let file of result) {
if (file.isDirectory == true && file.name != '.' && file.name != '..') {
} else if (file.isFile == true) {
let name = file.name // File name
let path = file.fullPath // File path
let file_size='' //file size
let file_datetime= '' // file date time
file.getMetadata(function (metadata) {
file_size = metadata.size; // Get file size
file_datetime = metadata.modificationTime;
this.getFileDetail={size:file_size,datetime:file_datetime}; // can't assign this global valiable
console.log(file_size,file_datetime); // function inside working fine
})
console.log(this.getFileDetail); // i can't access
console.log(file_size,file_datetime); // function outside not working
}
}
});
请让我知道如何将这个.getFileDetail放在一边。
答案 0 :(得分:0)
您可以实施以下承诺
getFileDetails() : Promise<boolean> {
return new Promise((resolve, reject) => {
this.file.listDir("file:///storage/emulated/0/", 'myfolder').then((result) => {
for (let file of result) {
if (file.isDirectory == true && file.name != '.' && file.name != '..') {
} else if (file.isFile == true) {
let name = file.name // File name
let path = file.fullPath // File path
let file_size='' //file size
let file_datetime= '' // file date time
file.getMetadata(function (metadata) {
file_size = metadata.size; // Get file size
file_datetime = metadata.modificationTime;
let fileDetails ={size:file_size,datetime:file_datetime}; // can't assign this global valiable
console.log(file_size,file_datetime); // function inside working fine
resolve(fileDetails);
})
console.log(this.getFileDetail); // i can't access
console.log(file_size,file_datetime); // function outside not working
}
}
});
})
getCallerAudio(){
this.getFileDetails().then(val => {
this.getFileDetail = val;
})
}