Unlocking Node.js v7: How async/await Simplifies Asynchronous Code
This article explains how Node.js v7’s first RC introduces async/await support, demonstrates its usage with code examples, compares it to traditional Promise handling, and shows how to enable the feature via command‑line flags or nvm for a smoother asynchronous programming experience.
With Node.js v6 released as v6.9.0 and entering LTS, Node.js v7 is approaching.
A few days ago, Node.js v7 released its first RC version, whose most notable feature is support for the async/await keywords, thanks to the V8 engine v5.4. To use these keywords, you can enable the feature via a command‑line flag: node --harmony-async-await What does async/await mean? In short:
async/await lets developers write code in a synchronous style while it actually runs asynchronously.
Essentially, async/await is a syntactic wrapper around Promises. For example, calling a function that returns a Promise, such as fetch, to asynchronously retrieve a resource:
const fun = function(url) {
return fetch(url).then(function(res) {
// prints 200
console.log(res.status);
})
};
fun('https://s.taobao.com');If you write it with async/await:
const fun = async function(url) {
const res = await fetch(url);
// prints 200
console.log(res.status);
};
fun('https://s.taobao.com');In more complex real‑world usage, async/await is simpler and more direct than using Promises.
Want to try it? You can download the RC packages from https://nodejs.org/download/rc/v7.0.0-rc.1/, but a more convenient method is to use nvm:
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/download/rc nvm install 7After the official release, you can simply run nvm install 7 to install it.
As a curious and diligent programmer, why not read the original article to learn more about async/await?
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Node Underground
No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
