It seems like you're mixing two different styles of handling asynchronous code: using .then()/.catch() and await. When using await, you should not use .then() or .catch() because await will pause the execution until the promise is resolved or rejected, and you should handle the response or error with try/catch.
Here's how you can refactor your code to use await properly:
async function postTodoItem() {
try {
// This will wait for the axios post request to complete before moving on.
let response = await axios.post('http://localhost/api/saveTodoItem', apiData, options);
console.log('Response from first request:', response);
} catch (error) {
console.error('Error in first request:', error);
}
// No need to use vi.waitFor here since await already waits for the promise to resolve.
console.log('OK NEXT TEST');
try {
// This will wait for the second axios post request to complete before moving on.
let resp = await axios.post('http://localhost/api/saveTodoItem', apiData, options);
console.log('Response from second request:', resp);
} catch (error) {
console.error('Error in second request:', error);
}
}
// Call the function
postTodoItem();
Make sure that your function is declared with async to use await inside it. This code will execute the first POST request and wait for it to complete before logging the response or error. Then it will proceed to the next test and do the same for the second POST request.
If your second request is still hanging, it could be due to several reasons:
- The server might not be handling concurrent requests properly.
- There might be an issue with the network or the URL you are trying to reach.
- The request might be taking longer than expected, and you might need to increase the timeout.
Make sure to check your Laravel logs for any errors and ensure that your server is configured to handle concurrent requests if necessary. Also, verify that your apiData and options are correctly formatted and that the server is capable of processing them.