0

I have narrow down my problem to this, I want to send post request to an API server but the post request sent only after the program exits. my main.js:

var request = require("request");
send_post_to_server = function(name, responseCallback, outconfig) {
    var outconfig = outconfig || {...};
    request.post({
        url: 'http://localhost:3000/api/backtest',
        json: outconfig
    }, function optionalCallback(error, response, body) {
        responseCallback(error, response, body);
    });
}

send_post_to_server ('first post request', my_response_callback);
send_post_to_server ('second post request', my_response_callback);
while(true); // with the loop, the server never gets the post requests.

If I remove the while loop and the program exits, it dose work and I do get the response to optionalCallback and responseCallback. But with the loop, the server dose not get the request. why is that happened? and how can I make the program send the request? some kind of flush? to run the program I use: node main.js and npm istall request for the request module.

roni
  • 59
  • 6
  • @cubrr I don't think this is my problem. in my case, if the program exits it dose execute all the requests. but with the loop, it looks like the program dose not execute the requests, like they have lower priority then the main thread if that makes any sence – roni Feb 29 '20 at 16:54
  • Yes, if you're in an endless execution loop, the callbacks are never handled because the event loop doesn't get a chance to run. – cbr Feb 29 '20 at 16:55
  • @cuberr so how can I 'flush' the event loop? to make sure I don't have any tasks. or how to give the program rest to send the requests? – roni Feb 29 '20 at 17:00
  • Your program will not exit while requests are still pending. node.js keeps going as long as their are pending asynchronous operations still waiting for completion with something listening for their completion. Remove the `while(true) {}` loop as it blocks the event loop so your requests cannot finish. `request.post()` is non-blocking and asynchronous, in case you didn't realize that. – jfriend00 Feb 29 '20 at 18:28

1 Answers1

0

Because while(true); is blocking the thread and Node.js using single thread. Prefer asynchronous to do the operation. See also: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

If you want to keep the Node.js process not terminating there is an answer for this How to forcibly keep a Node.js process from terminating?

Onikur
  • 176
  • 5
  • Thank you for your answer, I read what you send but I still could not understand where i'm dealing with async functions, I only want that as soon as the 'send_post_to_server ' function returns, the request will be sent, I don't mind waiting, but I don't know how to. – roni Feb 29 '20 at 18:10
  • Node.js will wait for all async events (`request.post({}, callback)` the callback is async already) before exit. Therefore, you could get the result from `my_response_callback` and do something with the response data. Just like below @jfriend00 what to say. – Onikur Mar 01 '20 at 01:57
  • I made an example based on your `main.js` https://runkit.com/onikur/stackoverflow-60467389-question-edit-1 – Onikur Mar 01 '20 at 02:10