1.async_demo completed
(
Async is a Process Control toolkit that provides direct and powerful asynchronous functionality. JavaScript is designed for node. JS and can also be used directly in the browser.
Async provides approximately 20 functions, including commonly used maps, reduce, filter, ForEach, etc., asynchronous process Control modes include, serial (series), parallel (parallel), waterfall (waterfall), etc.
)
Basically understand, also know how to use. But I forgot it after reading ... Just watch the demo when you can use it.
2.async await
originated in C #. There may be es7 inside.
Async function Showstuff () { var data = await loaddata ()//LoadData returns a Promise console.log (data)//data has been loaded Bi}async function () { await showstuff ()//Async function returns a Promise by default, so you can await another async function //Here Showstuff is done} Yu Yuxi Link: https://www.zhihu.com/question/25413141/answer/30767780 Source: Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
3.promise
So much has been said, what is promise?
In fact, promise is a class, and this class has become the standard of ES6, this class is currently in Chrome32, Opera19, Firefox29 above the version has been supported, want to use on all browsers on the words to see es6-promise it.
How does the promise use it?
For a very simple code, be careful to read the comments in the code.
var promise = new Promise (function (resolve, reject) { //do a thing, possibly async, then ... if (/* Everything turned out fine */) { resolve ("Stuff worked!"); } else { reject ("It broke"); }});
var val = 1;//We assume that Step1, Step2, Step3 are both Ajax call back ends or are//asynchronous operations querying the database on node. js//Each step has corresponding failures and successful processing callbacks//requirements So, Step1, Step2, Step3 must perform function Step1 (resolve, Reject) {Console.log (' Step One: Execute ') sequentially; if (val >= 1) {resolve (' Hello I am '); } else if (val = = = 0) {reject (Val); }}function Step2 (Resolve, Reject) {Console.log (' Step Two: Execute '); if (val = = = 1) {Resolve (' Hello I am No.2 '); } else if (val = = = 0) {reject (Val); }}function Step3 (Resolve, Reject) {Console.log (' Step Three: Execute '); if (val = = = 1) {Resolve (' Hello I am No.3 '); } else if (val = = = 0) {reject (Val); }}new Promise (STEP1). Then (function (val) {Console.info (val)); return new Promise (STEP2);}). Then (function (val) {Console.info (val); return new Promise (STEP3);}). Then (function (val) {Console.info (val); return Val;}). Then (function (val) {Console.info (val); return Val;}); /after execution will print step one: Perform hello I am first step two: Execute hello I am No.2 step three: Execute hello i am no.3hello I amNo.3
What problem does promise solve?
As shown in the above code, the author thinks that the meaning of promise lies in the then chain call , which avoids the layer nesting between asynchronous functions and transforms the nesting relationship of the original asynchronous function into a chain-step relationship that is easy to read and understand.
The main use of promise is to encapsulate individual asynchronous operations into many promise, while a promise only handles one asynchronous logic. Finally, each promise is concatenated with chained invocation notation, so that if the relationship between asynchronous logic is heavy, you do not need to nest layers, just encapsulate each asynchronous logic into a promise chain call.
Promise common key points when promise is defined, the function has been executed
The Promise constructor accepts only one parameter, which is a function with asynchronous logic. This function new Promise
has been executed at the time. It is just then
not resolve or reject before it is called.
How do I return in the Resolve method in then?
The then method typically passes two parameters, a resolve
function, and a reject
function. For the reject
time being, it is the function that runs when the error occurs. The resolve
function must return a value in order to make the chained call go down, and this value returns what is very much fastidious.
Returning a new promise and then calling then is the logic in the new promise.
Returns a value that is passed to the next-then resolve method parameter.
4.lesson6
- Learn to use the test framework mocha:http://mochajs.org/
- Learn to use Assertion Library Should:https://github.com/tj/should.js
- Learn to use the test rate overlay tool Istanbul:https://github.com/gotwarlost/istanbul
- Simple Makefile writing: http://blog.csdn.net/haoel/article/details/2886
Mocha and Istanbul Joint testing, test coverage comprehensive testing. Should is the assertion library, expect is also the assertion library, but more complex.
This is the legendary test-driven development .
2016.5.10