Issue downloading multiple csv files in app loader

Posted on
  • Hi,

    I'm generating several csv files on the bangle.js. Once it's done, I would like to make a convenient tool to download all files at the same time. For that, I'm creating a html tool for the app loader.
    I followed the existing examples and start with:

    <html>
     ...
    var csvData = "";
    
    function getData() {
      fileData = [];
      Util.showModal("Loading...");
      dataElement.innerHTML = "";
      var promise = Promise.resolve();
      Puck.eval('require("Storage").list(/data-............\.csv/)',files=>{
        if (files.length==0) {
          dataElement.innerHTML = "<p>No saved data</p>";
        } else {
          files.forEach(file => {
    

    This works and finds all my files. Then, in the loop, I would like to find a way to download everything at once. A satisfying way would be to concatenate all strings in the variable csvData, and then save it into a unique csv file on my computer. But definitely the best solution would be to save all files individually by pushing one button.
    But I couldn't make it work at all. I'm also very confused about how the code is executed. The code after the loop "forEach" always finish after the code bellow despite not using async.

    Could you give me some direction on how to solve my issue?

    Thanks!

  • The issue is that while you may not be using async stuff yourself, Puck.eval is async behind the scenes.

    I think the code you're after is actually already created as part of accelrec which has almost identical code to yours anyway: https://github.com/espruino/BangleApps/blob/c99e4e53a8cb73220cbcd2f1950aafcbda89a268/apps/accelrec/interface.html#L38

    Basically you want to make sure you execute everything one after the other, so create a promise with var promise = Promise.resolve(); (which you have done) and then in the forEach function do:

    promise = promise.then(function() {
       return new Promise(resolve=>{
        // do stuff in here, call resolve() when ready
      });
    });
    

    Finally you can call promise.then(function() { .... }) again to have that code run when all files are read

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Issue downloading multiple csv files in app loader

Posted by Avatar for user116444 @user116444

Actions