vue understand usage in return

Sometimes an emergency project in doing the project, we only need to know how to use what method to achieve the corresponding function, did not have to stay down research to understand why such a use, so take advantage of today's busy write about their understanding of the return of a aspect is to deepen their understanding of the other, hoping to help people in need!

I would like to throw a simple question: can later return with another statement or function it?

Answer: Yes!

But one thing, return no matter what, are the direct return, even if the statement or function will not be executed! This function is the function returns!

The following example from a return to a better understanding of usage:

export function getTable() {
    return axios.get('xxxx’  
    ).then(function (res) {    
       return  res.data.result.info 
   }).catch(function () {
   })
}
In axios and ajax often used in return, you can see from the above code there are two return, the role of each return all need to understand
First, with the first rear return axios is disposed, it is to return the entire axios out return; the second return is successful after data is returned in response to a request axios out, i.e., the second is to return the requested data returns asynchronous axios come out.
These two points are better understood, but do not know if you have ever wondered why do this?
About my understanding:
There have function and axios asynchronous and synchronous ajax call is asynchronous if there if there is return, when the outer function (function refers to getTable ()) call will fail to get the data, unless replaced synchronization. If you want to take the asynchronous data, it is necessary also to add functions return the outer layer, which is equivalent to the entire configuration axios ajax or return it to return out of the data format is [object, promise], so the function is executed to obtain the data format of [ object, promise], then .then () returns the form of the data inside out!
mounted() {
  getTable (). the then ((RES) => = RES this.tableData {}) // This getTable the first call () to get axios data, then it results in .then ()
}
This code is the object of the asynchronous request return result axios out, because it can not return directly in the asynchronous axios in the data, it is necessary to add also the return axios outer layer, so that you can get.

These are personal understanding! There are not welcome criticism correction!

Guess you like

Origin www.cnblogs.com/jennydtt/p/12607011.html