http request 'on end' not respecting promises



  • Not 100% sure what the issue here but if I put a call to a function that returns a promise inside the request.on('end'... the server hangs up the socket with the client before the promise has executed. I tried to do the same with with vanilla NodeJS and it works as expected. Source code below for Low.js and NodeJS. Spent half a day trying to get to the bottom of this. To test this, just go a POST from Postman. The Low.js version gives
    0edcb1d8-59da-4027-b966-554691b0f06f-image.png

    Low.js code

    import http = require('http');
    import fs = require('fs');
    
    function sleep(milliseconds) {
        return new Promise(resolve => setTimeout(resolve, milliseconds));
    }
    
    http.createServer(function (req, res) {
    
        if (req.method === "GET") {
            res.writeHead(200, { "Content-Type": "text/html" });
            fs.createReadStream("./client.html", "UTF-8").pipe(res);
        } else if (req.method === "POST") {
    
            let body = "";
            req.on("data", function (chunk) {
                body += chunk;
            });
    
            req.on("end", function () {
                sleep(1000)
                    .then(() => {
                        res.writeHead(200, { "Content-Type": "text/html" });
                        res.end(body);
                    });
            });
        }
    
    }).listen(3000);
    

    NodeJS sample code.

    var http = require('http');
    var fs = require('fs');
    
    function sleep(milliseconds){
           return new Promise(resolve => setTimeout(resolve, milliseconds));
    }
    
    var server = http.createServer(function (req, res) {
    
        if (req.method === "GET") {
            res.writeHead(200, { "Content-Type": "text/html" });
            fs.createReadStream("./client.html", "UTF-8").pipe(res);
        } else if (req.method === "POST") {
        
            var body = "";
            req.on("data", function (chunk) {
                body += chunk;
            });
    
            req.on("end", function(){
    			sleep(1000)
    			.then(()=>{
    				res.writeHead(200, { "Content-Type": "text/html" });
    				res.end(body);				
    			});
            });
        }
    
    }).listen(3000);
    


  • Any reason why you have 2 versions here? The Node.JS version would work just as well with low.js.. Well, with the socket hangup, I guess..

    Will fix the socket hangup in the next version which I will latest finish next weekend. So max 7-8 days.



  • I am building in typescript so the one above is the .ts. I actually tried the .js in both environments and the same results.



  • Which is expected. Will tell you when it is fixed.



  • Fixed in new version 1.6.2. Can be flashed with lowsync now, or updated via IDE with the neonious one. The PC versions will be available for download sometime today or tomorrow.

    -- Thomas



  • Working great! Thanks for the fix.


Log in to reply