by drakinite » Thu Jun 02, 2022 10:28 pm
1 -
track.loadCoverListAsync() is actually poorly named. It does not return a promise; instead, it returns the CoverList immediately. So when you're awaiting track.loadCoverListAsync(), the await does nothing. (If you await a non-Promise variable, it'll just return that variable instantly.)
2 -
list.count only becomes valid
after you
await list.whenLoaded(). If you do not wait until it is loaded, sometimes the count will be zero and sometimes it will be correct (or even perhaps a number in between 0 and the correct value).
3 -
covers.locked(),
covers.getValue, and
exportCover.saveToFile() are all synchronous. Do not await them (same reason as #1).
Should look something akin to this:
Code: Select all
saveCoverToFile: async function(track, path){
// exports first cover of track
var _this = this;
var covers = track.loadCoverListAsync();
await covers.whenLoaded();
console.debug(`covers.count= ${covers.count}`);
if(covers.count > 0){
covers.locked(async () =>{
var exportCover = covers.getValue(0);
var ext = await _this.getCoverFileType(exportCover.pictureType);
var exportPath = `${path}\.${ext}`;
exportCover.saveToFile(exportPath);
});
}
},
If you prefer one-liners, the following snippet is also valid (because you're awaiting the promise that whenLoaded() returns, not the list returned by loadCoverListAsync()):
Code: Select all
var covers = await track.loadCoverListAsync().whenLoaded();
1 - [b]track.loadCoverListAsync()[/b] is actually poorly named. It does not return a promise; instead, it returns the CoverList immediately. So when you're awaiting track.loadCoverListAsync(), the await does nothing. (If you await a non-Promise variable, it'll just return that variable instantly.)
2 - [b]list.count[/b] only becomes valid [i]after[/i] you [b]await list.whenLoaded()[/b]. If you do not wait until it is loaded, sometimes the count will be zero and sometimes it will be correct (or even perhaps a number in between 0 and the correct value).
3 - [b]covers.locked()[/b], [b]covers.getValue[/b], and [b]exportCover.saveToFile()[/b] are all synchronous. Do not await them (same reason as #1).
Should look something akin to this:
[code]
saveCoverToFile: async function(track, path){
// exports first cover of track
var _this = this;
var covers = track.loadCoverListAsync();
await covers.whenLoaded();
console.debug(`covers.count= ${covers.count}`);
if(covers.count > 0){
covers.locked(async () =>{
var exportCover = covers.getValue(0);
var ext = await _this.getCoverFileType(exportCover.pictureType);
var exportPath = `${path}\.${ext}`;
exportCover.saveToFile(exportPath);
});
}
},
[/code]
If you prefer one-liners, the following snippet is also valid (because you're awaiting the promise that whenLoaded() returns, not the list returned by loadCoverListAsync()):
[code]
var covers = await track.loadCoverListAsync().whenLoaded();
[/code]