How to wait for button click in Promise chain to pause execution?
P粉170438285
2023-08-15 13:06:43
<p>I'm trying to chain some backend calls to process files, but the whole workflow requires the user to provide some input halfway through. I'm not sure how to pause execution until the user clicks a button "<code>Continue</code>" so that it can continue working. </p>
<p>So the process is as follows:</p>
<ol>
<li>The user selects a file and triggers an event to upload some data. </li>
<li>I get the response from the previous call, opening a modal with a form. I need to pause here. </li>
<li>Fill out the form and click the <code>Continue</code> button to continue the promise chain. </li>
<li>Trigger another call to submit more information to another endpoint. </li>
</ol>
<p>So each of these steps involves an HTTP request, using <code>axios</code>, but I'm having trouble understanding how to chain these promises. </p>
<p>Now I have code similar to: </p>
<pre class="brush:js;toolbar:false;">onFileSelected(event) {
// code here
axios
.post("")
.then((res) => {
//Here I need to open the modal box and wait for the button to be clicked
})
.then(() => {
anotherMethod();
});
}
</pre>
<p><br /></p>
Promise itself does not provide a method to pause execution, but you can use async/await syntax to achieve this. Create a custom Promise that resolves when the user clicks the "Continue" button. like this: