A nice async / await, generator / yield

tags: web

  1. async/await
    async function is a function of the async keyword declared. async AsyncFunction function is an example of the constructor, and which allows await keyword. async and await keywords so that we can use a more concise way to write asynchronous behavior based Promise, without deliberately chained calls promise.
    await operator to wait a Promise object. It can only be used in asynchronous function async function.

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)
  1. generator/yield
    This function * declaratively (function keyword followed by an asterisk) defines a generator function (generator function), which returns a Generator object.
    yield keyword to pause and resume a generator function ((function * legacy or generator function).

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 }

Intelligent Recommendation

From generator to async/await

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...

Promise Generator Async/await

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...

co generator async await

async await co async await...

Promise Generator 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...

Promise/Generator/Async/await

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...

More Recommendation

Generator and Async, Await

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...

generator+co+async+await

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...

Generator and Async / Await

  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

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 and async and await

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top