How can I block until user-input received?

£可爱£侵袭症+ 提交于 2019-12-10 19:53:00

问题


I am trying to implement the following script:

A function tries to execute an asynchronous call, and if an exception is thrown then the user is prompt to input whether or not the function should run again.

If the user inputs "y", then the procedure should repeat.

If the user inputs "n", then the procedure should terminate.

If neither, then the question should repeat.

The execution of my entire script should block until either "y" or "n" are input by the user.

Here is what I have so far (with the help of this answer):

async function sendSignedTransaction(rawTransaction) {
    try {
        return await web3.eth.sendSignedTransaction(rawTransaction);
    }
    catch (error) {
        process.stdout.write(error.message + "; try again (y/n)?");
        process.stdin.on("data", async function(data) {
            switch (data.toString().trim()) {
                case "y": return await sendSignedTransaction(rawTransaction);
                case "n": process.exit();
                default : process.stdout.write("try again (y/n)?");
            }
        });            
    }
}

The problem is that the execution of the script continues without waiting until the user has inputted either "y" or "n".

Any help will be very much appreciated.

Thank you!!!


回答1:


That's because process.stdin operations are also asynchronous so wherever you invoke the initial sendSignedTransaction, if it throws an error there is nothing (currently) in that block of code that stops the function from exiting.

You have a mixture of Promise & classic callback code. If you want to make sure the caller waits until the function has completely finished, then you can convert the full function into a Promise which gives you more control over it e.g.

function sendSignedTransaction(rawTransaction) {
  return new Promise(async (resolve, reject) => {
    try {
      const result = await web3.eth.sendSignedTransaction(rawTransaction);
      return resolve(result);
    } catch (e) {
      process.stdout.write(e.message + "; try again (y/n)?");
      process.stdin.on('data', async data => {
        switch (data.toString().trim()) {
          case "y": return resolve(await sendSignedTransaction(rawTransaction));
          case "n": return process.exit();
          default : return process.stdout.write("try again (y/n)?");
        }
      });
    }
  });
}


来源:https://stackoverflow.com/questions/54060882/how-can-i-block-until-user-input-received

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!