The promise object returned by js has a value, why is the returned value ""?

The problem I discovered when processing js today is that the returned promise object has a value, but when I return the attribute value inside, it is always an empty string ""

Insert image description here
When returning result.data,
Insert image description here
I searched for the reasons and found the following:

In JavaScript, the Promise object is a mechanism for representing the results of asynchronous operations. It has three states: pending (in progress), fulfilled (successful) and rejected (failed). The value of the Promise object is not set immediately at the beginning, but is set to the successful value (fulfilled) or the reason for failure (rejected) after the asynchronous operation is completed.

If you return a value in a Promise object's callback function, and that value becomes fulfilled, you should be able to access that value later in the Promise chain. If the returned value in your code is an empty string "", there may be several reasons:

The asynchronous operation has not yet completed : If the value of the Promise is accessed in the callback function of the Promise object, but the Promise object has not yet completed the asynchronous operation, the value may be an empty string. Make sure to access the value of the Promise after it has completed.

Asynchronous operation fails : If the Promise object eventually becomes rejected, the value returned in the Promise chain will no longer be the success value, but the reason for the failure. This may cause the returned value to be an empty string, depending on why your asynchronous operation failed.

Improper exception handling : If exceptions are not handled appropriately in the Promise chain (through .catch or try...catch), it may cause a Promise in the Promise chain to be rejected, thus affecting the value of subsequent Promise.

Be sure to check the state of individual Promise objects in your Promise chain as well as exception handling to better understand why the returned value is an empty string.

solve

The method is added. await
Insert image description here
It may be returned before the query is found at the beginning. Add the keyword await to solve the problem.

Guess you like

Origin blog.csdn.net/wang121213145/article/details/133020008