Deep Understanding JavaScript Asynchronous Series (5)--async Await__java

Source: Internet
Author: User
Tags generator
the first part, the introduction of async-await in ES7

Original address http://www.cnblogs.com/wangfupeng1988/p/6532734.html without the author's permission, may not reprint ~

Before the introduction of generator asynchronous processing, can be said to be stumbled, after a variety of basic introduction and encapsulation, easy out a relatively simple asynchronous processing scheme, learning cost is very high ———— it is obviously not what we want.

As a result, ES7, which has not yet been released, simply encapsulates a set of asynchronous processing schemes ———— async-await for its own reference generator. Said is the reference, in fact, can be understood as generator of the grammatical sugar.

The sample code in this section provides an overview of the differences and benefits of using async-await in contrast to the generator and async-await in this section next ... comparison of generator and async-await

First to a section of generator processing asynchronous code, has been introduced before, can not understand the acquisition and then look.

Co (function* () {
    const R1 = yield readfilepromise (' Some1.json ')
    console.log (R1)  //print 1th file content
    Const R2 = Yield readfilepromise (' Some2.json ')
    console.log (R2)  //Print 2nd file contents
})

Here's another async-await. The execution code is as follows, which makes a comparison.

Const Readfilepromise = q.denodeify (fs.readfile)

//define Async functions
Const READFILEASYNC = Async function () {
    cons T f1 = await readfilepromise (' Data1.json ')
    const F2 = await readfilepromise (' Data2.json ')
    console.log (' Data1.json ', f1.tostring ())
    console.log (' Data2.json ', f2.tostring ()) return

    to ' done '//ignore First, later on
}
/ /execute
Const result = Readfileasync ()

From the above two sides of the code comparison, async function instead of function*,await replaced the yield, the other is no different. Oh, also, use async-await time no longer reference co such a third party library, direct execution can. the differences and benefits of using async-await

First, await can no longer follow the thunk function, but must be followed by a Promise object (therefore, promise is the ultimate asynchronous solution and the future). It is OK with other types of data, but it is executed directly, not asynchronously.

Second, the execution of const result = Readfileasync () Returns a Promise object, and the return ' done ' in the above code is directly received by the following then function

Result.then (data => {
    console.log (data)  /Done
})

Third, from the readability of the code will, async-await more easy to read the introduction, but also more in line with the semantics of the code. And there is no need to refer to a third-party library, nor do you need to learn generator that pile of things, the use of very low cost.

Therefore, if the ES7 is officially released, it is strongly recommended to use async-await. But it has not yet been formally released, from the stability of the consideration, or generator better. Next ...

The node V7 version has already started native support Async-await, but the current stable version of node is V6, not yet supported, what to do. ————, of course, is the Babel of the Almighty. The next section describes.

The second part, how to use the async-await in the Nodejs v6.x version

This section describes how to use Babel to enable the node V6 version to run async-await This section provides an overview of installing the necessary plug-ins to create a portal file and perform the necessary plug-ins for installation

Run NPM i babel-core babel-plugin-transform-runtime babel-preset-es2015 babel-preset-stage-3 babel-runtime-- Save to install a bunch of required plug-ins.

The. babelrc file is then created in the project root, and the contents of the file are written as

{
  "presets": ["stage-3", "es2015"],
  "plugins": ["Transform-runtime"]
}
Create the Portal file and execute

To join the code file you write Async-await is test.js, then you need to create another file, such as test-entry.js as the entry file. The entry file content is written as

Require ("Babel-core/register");
Require ("./test.js");

Then you can run the node test-entry.js directly.

the third part, the overall summary

A week or so of spare time summed up, finished, but also tired me. is not a physical life, but every day sitting at the desk writing these things is also a test of a person's strength, no patience is certainly not ———— this is the acceptance speech?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.