Generators / yield (es2015) and Async / await (es7) difference

Generators / yield (es2015) and Async / await (es7)

function* foo () {  
  var index = 0;
  while (index < 2) {
    yield index++;
  }
}
var bar =  foo();

console.log(bar.next());    // { value: 0, done: false }  
console.log(bar.next());    // { value: 1, done: false }  
console.log(bar.next());    // { value: undefined, done: true }  

co(function* (){  
  yield Something.save();
}).then(function() {
  // success
})
.catch(function(err) {
  //error handling
});


async function save(Something) {  
  try {
    await Something.save()
  } catch (ex) {
    //error handling
  }
  console.log('success');
} 

Intelligent Recommendation

ES7 of Async / Await

   basic rules async representationThis is an async function,await this function can only be used inside。 await expressed hereWait for the promise to return results, And then continue. await followed ...

ES7 -- async+await

Introduction to async: async function is a generator of sugar, it can be seen that it is realized by the idea of ​​Generator + Promise + Co after compiled by babel. Purpose: To solve the problem of as...

Async and await in ES7

Async and await in ES7 In the previous chapter, Promise was used to convert the original callback method into a chain operation, which put the asynchronously executed operations on a synchronous line....

ES7 detailed Async/Await

Async/Await is a new feature of ES7 standard that has not been officially announced. In short, it allows you to write asynchronous code in a synchronous way. For the front end, the writing of asynchro...

ES7-async and await

Last blog usePromise and Generator combinationRealizeAsynchronous code style is written as synchronous code styleIn fact, this method is considered obsolete in ES7 syntax. So how to implement the ES7 ...

More Recommendation

ES7 async/await application

What is async/await? Async means "asynchronous", await is short for async wait, as the name suggests, we can understand it as waiting for asynchronousfunctionThe execution is complete async&...

ES7 Async, Await

He did the ASYNC, AWAIT, and finally, the land, people and came acetab like today. Let's take a list first code segment: Output results: Question: Response actually got the promise's resolve return va...

FLATScript: ES7 `async` /` Await`

2019 Unicorn Enterprise Heavy Glour Recruitment Python Engineer Standard >>> After I experienced the Callback Hell of JSPromiseAs I said that this is still very good to solve some particular ...

ES7 async / await

ES7  async / await   ...

ES7 Features --Async / Await

First, Async Async is the keyword related to asynchronous operations, and promise, Generator has a lot of association with promise. Async keyword returns a promise object, if it is a normal function, ...

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

Top