0

Is there a way to hang/pause the function in the middle of its execution and then continue after few things are handled(input from the user received through socket.io) in node js? I know this is bad practice and need to use promises, async/await etc. However, according to the functionality of my program, I need to handle it this way.

My original question where I shared my code is here: Node application interactively get user input from react front end by pausing the current execution

Ali Beyit
  • 431
  • 1
  • 6
  • 19
  • If you're going to ask for help with a "bad practice", you should put a little more effort into explaining _why_ you need to do it that way, rather than just "trust me, I need to do it this way". – JLRishe Feb 10 '19 at 19:46
  • don't think it's possible to pause a node thread. The best you can do might be the `sleep` node implementation from [this](https://stackoverflow.com/questions/14249506/) question –  Feb 10 '19 at 19:47
  • 1
    This is XY problem. There are virtually no valid reasons to do this, and older question confirms this. Stick to it and consider offering a bounty in case it won't get enough attention for an answer within several days. – Estus Flask Feb 10 '19 at 21:01

4 Answers4

1

... "pause the function in the middle of its execution" is unlikely to really be describing what you want to have happen. I assume you have some asynchronous code running that is responsible for getting your program to a point where "a few things are handled" ... so you code looks something like

  var a_few_things_have_been_handled = false;
  handle_a_few_things(); 
  // which returns immediately but has a side effect of 
  // effectively setting a_few_things_have_been_handled to true

  while(!a_few_things_have_been_handled) {
    // do nothing just wait...
    // actually you want to yield to asynchronous threads
    // but you can't do it like this
  }

  the_rest_of_your_program();

Unfortunately that's not how the language works... you have to restructure your program flow to be explicit about sequential program flow using Promises or similar asynchronous flow control constructs.

vicatcu
  • 5,407
  • 7
  • 41
  • 65
  • That was the answer I was looking for. I need to restructure the flow of the game I suppose if there is no way of handling my need. You can check out my original question from the link provided if you can give further hints about how to restructure it accordingly. – Ali Beyit Feb 10 '19 at 19:54
0

You can wrap "second block" of your function in setTimeout() call, like this:

function f()
{
  statement1;
  setTimeout(() => {
    statement2;
  }, 1000);
}
Tom HANAX
  • 402
  • 3
  • 17
0

you can do it using async/await feature in javascript. rewrite any callback-based function to use Promises, then await their resolution.

Faisal Ahmed
  • 350
  • 3
  • 7
0

You can do that with async,await and Promise.

function func1(){
  console.log("function 1 is finished")
}
function func2(){
  console.log("function 2 is finished")
}

const thingsHandled = new Promise((res,rej) => {
    func1();
    func2();
    console.log("Every function is done")
    res();
})
   
async function main(){
    await thingsHandled;
    console.log("function is continued");
}
main();
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73