• @mike_k, I understand your frustration... first you give yourself the run-around, and then everyone else does it to you. As much I agree with that @Gordon has most of the insight, there are many others - when it comes to the non-Espruino boards and especially to espressif based boards - who did the ports and spend their time to push Espruino forward on those platforms. This was and is the case for ESP8266 boards, and is even more so for ESP32 boards, and is also mirrored in how @Gordon allocates support resources.

    So far, I did not use fs or File to that extend that I can answer all questions. I appreciate your thorough exploration of all paths before tapping into time of others. We all know about good and not so good 'forum citizens'...and the one being lazy. Trying and verifying all avenues upfront help debug an issue. Therefore, I think it has nothing to do with how the code gets uploaded or when and how its execution gets started.

    The last thing I could think of is that somehow resources are not released. This can happen due to Javascript being single threaded. I think though that you already took care of that by the setTimeout() you use for every next iteration. I doubt that changing - increasing significantly - the timeout time to give the system even more time to do what it has to do to enables writing more than just ten (10) files. It could though be worth - as for a last try - to separate the loop control from the loop body. I do not know how resources acquired in a function are handled when recursion is involved, even with setting the direct self-invocations apart with setTimeout ().

    I suggest code like that - in which I even try to reference the loop body function not by symbol reference but string reference - in two different ways: lines 16 and 18. (Not sure if it even matters for Espruino's JavaScript implementation since it runs on the source anyway, and second one is not necessarily the best from a security point of view, since it involves eval().)

    EDIT ...this code has a fundamental flaw - thanks @Wilberforce to point that out in your following post. Therefore, take the code as sh0wn in my next post, post #8.

    var fs = require('fs');
    
    var maxFileNum = 100, fileNum = 0
    
    function writeFile0(i){
       var data = Math.random().toString();
       var name = `file_${i}`;
       var res = fs.writeFile(name, data);
       if (! res) {
         console.log(`did not write!, ${name}, ${data}`);
       }
      }
    }
    
    function writeFile() {
      global["writeFile0"](fileNum);
      if (++fileNum < maxFileNum) {
        setTimeout("writeFile()", 100);
      }
    }
    
    function onInit() {
      writeFile();
    }
    
    setTimeout(onInit, 1000);
    

    Since cutting the writings into separate JavaScript execution streaks withsetTimeout() didn't help, you are welcome to look under the hood and suggest changes to fs component to overcome what ever limitation you obviously discovered... after all, it's all open software...

    Btw, with fs there is neither open nor close, only write and append. The open/write/close pattern is implemented with File.

About

Avatar for allObjects @allObjects started