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".