tags: web
Code Example:
function resolveAfterManySeconds(duration) {
return (new Promise(resolve => {
setTimeout(() => {
resolve(duration);
}, 1000*duration);
})).then(function(x){
return x*100;
});;
}
async function f1() {
console.log(new Date());
var x = await resolveAfterManySeconds(1);
var y = await resolveAfterManySeconds(4);
console.log(x); // 1
console.log(y); // 4
console.log(new Date());
}
f1(); // Reference console.log, 5 seconds Back
The results demonstrate:
Sun Feb 21 2021 16:49:50 GMT+0800 (China Standard Time)
async.html:24 100
async.html:25 400
async.html:27 Sun Feb 21 2021 16:49:55 GMT+0800 (China Standard Time)
Code Example:
function* countAppleSales () {
var saleList = [3, 7, 5];
for (var i = 0; i < saleList.length; i++) {
yield saleList[i];
}
}
var appleStore = countAppleSales(); // Generator { }
console.log(appleStore.next()); // { value: 3, done: false }
console.log(appleStore.next()); // { value: 7, done: false }
console.log(appleStore.next()); // { value: 5, done: false }
console.log(appleStore.next()); // { value: undefined, done: true }
review The so-called asynchronous is that part of the program is now running, while the other part is running in the future. The focus of asynchronous processing is how to handle that part of the futu...
1. Promise object Before , there are probably the following four methods for asynchronous programming: callback function, event monitoring, publish / subscribe, Promise object The callback function it...
async await co async await...
Promise(ES6) 1. Represents asynchronous operation, there are three states: Pending (in progress, unfinished) Resolved (completed, also known as Fulfilled) Rejected (failed) 2. Advantages: Solve the pr...
Async / await is through waiting for execution, instead of being blocked, the difference between him, and synchronization is to block and non-blocking Why introduce async As is well known, the executi...
async、await async Async is a story of "asynchronous", async is used to declare an asynchronous Function await AWAIT can be considered an abstract of Async Wait, and AWAIT is used to wait for...
Browser solves the history of asynchronous development Callback promise generator async+await generator Generator core source code When the user calls the Generator method, the internal package is pac...
introduction Contacting AJAX requests will encounter asynchronous calls, in order to ensure the correctness of the call order, we will usually call in the callback function, and use some new so...
async、await、generator Foreword First, Async and Await 1.async 2.await 3. Comprehensive application Second, Generator functions Basic syntax 2. You can use the return value after the FOR..OF traversed ...
Generator functions, define generator functions by adding a * number before the function name, returns an iterator object, then pass the function inside the functionyieldIdentification and iterator ob...