Home  >  Article  >  Web Front-end  >  Detailed explanation of obtaining the return value instance of JavaScript asynchronous function

Detailed explanation of obtaining the return value instance of JavaScript asynchronous function

怪我咯
怪我咯Original
2017-06-29 10:58:191604browse

This article will share with you how to solve the problem of obtaining the return value of JavaScriptAsynchronousFunction and the final solution. Friends who need it can refer to it

Study a small question today: How to get the return value of the JavaScript asynchronous function?

1. Wrong attempt

My initial attempt when I didn’t enter the industry:

2.Callback function

What popped up was not 4, but 0. Later I found out that this was an asynchronous problem.

We need to use callback technology to do it:

3.promise

The callback function is really a good thing, and I have been writing code like this for a long time. Pass the function when encountering asynchrony! ! Later, I learned that there is a thing called promise, which specifically solves problems caused by callback functions, and learned about promise:

The promise still did not give up the callback, but the position of the callback changed.

4.generator

Later I learned about generator and knew that it has the ability to interrupt function execution, so I made a new attempt:

Synchronous writing method can realize asynchronous logic, which feels much more advanced.

5.promise + generator

Later I heard that promise plus generator is the perfect way to asynchronously use anti-aircraft guns to kill mosquitoes (this example is not enough Tell me the benefits of using the two together):

6.async

#I thought this was cool enough, but later I heard that es7 provided The ultimate solution: async.

As a young man who loves to learn, he thinks that he cannot be left behind:

The above is the detailed content of Detailed explanation of obtaining the return value instance of JavaScript asynchronous function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn