triniwiz / nativescript-downloader

Apache License 2.0
32 stars 18 forks source link

Multiple Download in one go using async / await #52

Open leocrawf opened 4 years ago

leocrawf commented 4 years ago

Make sure to check the demo app(s) for sample usage

Make sure to check the existing issues in this repository

If the demo apps cannot help and there is no issue for your problem, tell us about it

Please, ensure your title is less than 63 characters long and starts with a capital letter.

Which platform(s) does your issue occur on?

Android Emulator

Please, provide the following version numbers that your issue occurs with:

I want to wait until all the files are downloaded before I show the start button but the button becomes active even before files are fully downloaded

let getFiles = async (paths) => {

let downloader = (args) => {

    return new Promise((resolve, reject) => {

        downloadManager = new Downloader();

        let promises = [];

        const documents = fs.knownFolders.documents();

        args.forEach((data, index) => {

            let file = exports.getFileName(data.pageUrl.downloadURL);
            let url = data.pageUrl.downloadURL;

            //check if this file was downloaded already
            const path = fs.path.join(documents.path,file);
            const exists = fs.File.exists(path);

            if (exists === false) { 

                let promise = downloadManager.createDownload({
                        path: fs.knownFolders.documents().path,
                        fileName: file,
                        url:url
                    });

                promises.push(promise);

            }

        });

        resolve(promises);
    })

}

let finishDownload = async (ids) => {
    page.bindingContext.downloadedFiles._array.splice(0);
    ids.forEach((data,index) => {
        downloadManager
        .start(data, (progressData) => {
            // console.log(data+" id: download value,",progressData.value);
            // console.log(data+" id: download speed,",progressData.speed);
        })
        .then(completed => {
            console.log(`downloaded File path: ${completed.path}`);

        }).catch(error => {
            console.log("downloadManager error: ",error.message);
        });
    })

   //this is always returned, even before files are downloaded.
    return "done";
}

let y = await downloader(paths);

let x = await finishDownload(y);

if (x === "done") {

    console.log("are we now finished!!!!!!!!");

    hideLoading();

    console.dir("all files are now downloaded and about to be saved!!!");

    let file = exports.getFileName(page.navigationContext.pages[0].pageUrl.downloadURL);

    //check if this file was downloaded already
    let documents = fs.knownFolders.documents();

    const path = fs.path.join(documents.path,file);
    const exists = fs.File.exists(path);

    if (exists) {
         modalContentButton.class = "modal-content-button-active";
    }
}

}

Can anyone assist with this? I think the issue is the finishDownload because it always returns before the files are downloaded and I cannot use an await before the downloadManager. If there is better way to do this please let me know.

leocrawf commented 4 years ago

The accepted answer here https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop did the trick.