• There's actually an example on using drain on the Internet page: http://www.espruino.com/Internet#transferring-large-amounts-of-data

    var history = new Uint8Array(10000);
    
    // ...
    
    function onPageRequest(req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      var n = 0;
      res.on('drain',function() {
        for (var i=0;i<10;i++)
          res.write(history[n++]+",");
        if (n>=history.length) res.end(" ];");
      });
      res.write("var data = [");
    }
    require('http').createServer(onPageRequest).listen(8080);
    

    It should be pretty easy to tweak your example to use that rather than setTimeout @DrAzzy - also, if you wanted you could use htypes=Object.keys(HISTORY) - so that then every field in your object gets enumerated. If you wanted to avoid having the counter you could do the above line and could then just remove items from htypes as they were output - and then stop when it was empty.

    Unfortunately async/await isn't in Espruino yet - its efficient implementation is particularly painful in Espruino. However honestly using Promises wouldn't be that much more typing and it's probably a lot more obvious what's happening. The current solution works fine without them though.

About

Avatar for Gordon @Gordon started