And if yes - how?
I have the following IONIC 3 code and I dont know why it works how it works
First of all there is a so called "MusicService" which is responsible for loading the music files from the local storage:
private searchList: string[] = ["sdcard/Music", "sdcard/Download"]
public LoadMusicFromFS(): Promise<void>{
this.foundMusic = []
return new Promise((resolve, reject) => {
this.searchList.forEach((dir)=>{
this.CheckDir(dir, 0)
})
})
}
public foundMusic: Music[] = []
private CheckDir(dir: string, level: number): Promise<void>{
return this.localFileSystem.listDir("file:///", dir).then((arr)=>{
arr.forEach((elem)=>{
if(elem.isDirectory){
if(!(this.ignoreList.indexOf(elem.fullPath.slice(1, -1))>-1)) this.CheckDir(elem.fullPath.substr(1), level+1)
}else{
let fileType: string = elem.name.split(".").pop()
if(this.searchTypes.indexOf(fileType)>-1){
console.log(elem.fullPath + " | " +elem.name+ " --> is Dir? "+elem.isDirectory)
let addingMusic: Music = new Music()
addingMusic.description.title = elem.name.split(".").slice(0, -1).join(".")
addingMusic.media.filePath = elem.fullPath
addingMusic.description.album.name="Test"
addingMusic.media.length=100
this.foundMusic.push(addingMusic)
}
}
})
}, err => {})
}
And there is also a Page where I call the function from the service:
this.platform.ready().then(() =>{
this.musicService.LoadMusicFromFS().then(()=>{
// This will never be printed -- why?
console.log("Music List successfully loaded")
})
// This loads the correct Music, but only because of references
this.musicCache=this.musicService.foundMusic
})
I dont really get it - why isnt the "then" part working in the Page?