求助如何获取以promise方式返回的值
getAllVolumes(): Promise>,异步获取当前外置存储中所有卷设备信息,以promise方式返回。 我的源码: import volumemanager from "@ohos.file.volumeManager"; @Entry @Component struct TFCardTest {
getAllVolumes(): Promise<Array<Volume>>,异步获取当前外置存储中所有卷设备信息,以promise方式返回。
我的源码:
import volumemanager from "@ohos.file.volumeManager";
@Entry
@Component
struct TFCardTest {
@State scanRes: Array<volumemanager.Volume> = []
TFScan() {
let rec: Array<volumemanager.Volume> = []
volumemanager.getAllVolumes().then(function (volumes) {
if (volumes.length > 0) {
rec = volumes
console.info( rec[0].id)
console.log('rec长度'+rec.length.toString());
console.info("success11")
}
}).catch(function (error) {
console.info("getAllVolumes failed" + JSON.stringify(error))
});
console.log('#####################');
console.log('rec长度'+rec.length.toString());
console.log('#####################');
}
aboutToAppear() {
this.TFScan()
}
build() {
Column() {
Button("scan")
.size({ width: 200, height: 75 })
.fontSize(30)
.onClick(() => {
this.TFScan()
console.log('scan******************************************');
})
}
.size({ width: '100%', height: '100%' })
}
}
运行打印log,两次打印rec长度不一致,跳出:volumemanager.getAllVolumes().then(function (volumes) {},无法获取volumes的值
10-20 15:16:54.185 28165-28165/com.example.kfjs_motherboardtest I A0fefe/JsApp: scan******************************************
10-20 15:16:54.187 28165-28165/com.example.kfjs_motherboardtest I A0fefe/JsApp: vol-179-100
10-20 15:16:54.187 28165-28165/com.example.kfjs_motherboardtest I A0fefe/JsApp: rec长度5
10-20 15:16:54.187 28165-28165/com.example.kfjs_motherboardtest I A0fefe/JsApp: success11
10-20 15:16:54.394 28165-28165/com.example.kfjs_motherboardtest I A0fefe/JsApp: #####################
10-20 15:16:54.394 28165-28165/com.example.kfjs_motherboardtest I A0fefe/JsApp: rec长度0
10-20 15:16:54.394 28165-28165/com.example.kfjs_motherboardtest I A0fefe/JsApp: #####################
更多推荐
所有评论(0)