At New Relic, we believe that application performance monitoring (APM) should be available for the latest and greatest technology platforms. And we have long been committed to supporting the Node.js ecosystem and providing our customers with timely support for the newest versions of Node.js.
Today we're announcing the release of Version 2.3.0 of the New Relic Node.js agent. With this release, our support for the async/await keywords with Node 8 moves out from behind its feature flag and is now enabled by default.
Published under the codename Carbon, Node 8 was originally slated for release by Nodejs.org in late April but was delayed to allow Node to integrate a version of Google’s V8 JavaScript engine that includes Ignition and TurboFan, the new JavaScript execution pipeline. We shipped our initial support for Node 8 shortly after its release at the end of May and have now updated the agent to include support for the use of Async/Await.
Node 8 is packed with features
Node.js 8.0.0 ships with JavaScript Engine V8 5.8, an update to the JavaScript runtime that includes major improvements in performance and developer-facing APIs. There is also plenty to be excited about with Node 8 itself.
Introducing N-API: One of the most significant new features is the new N-API module, which aims to provide a stable ABI (Application Binary Interface) across all future versions of Node.js. This new API is intended to insulate Addons from changes in the underlying JavaScript engine and allow native modules compiled for one version to run on later versions of Node.js without being recompiled.
Support for async/await: Another key addition is the introduction of the async and await keywords. While Node 7.6.0 was the first version to support async functions without a feature flag, Node 8 is the first LTS version to support it. Being an LTS release, it was quickly adopted by the community. In combination with the new await keyword, this makes using Promises even cleaner and easier to read.
Promises already represented a huge step forward in getting out of “callback hell.” Compare these two examples, both of which perform the same sequence of steps. Using the traditional callback style, we might write something like this:
function callbackStyle(cb) {
doSomething((err, something) => {
if (err) {
cleanupSomething()
return cb(err)
}
doSomethingMore(something, (err, more) => {
if (err) {
cleanupMore()
return cb(err)
}
doFinalSomething(more, (err, final) => {
if (err) {
cleanupFinal()
return cb(err)
}
cb(final)
})
})
})
}
function promiseStyle() {
return doSomething()
.then((something) => doSomethingMore(something))
.then((more) => doSomethingFinal(more))
.catch((err) => {
cleanupEverything()
throw err
})
}
With the new async functions built on top of Promises, this asynchronous code example can be rewritten to read just as cleanly as synchronous code, as shown here:
async function awaitStyle() {
try {
const something = await doSomething()
const more = await doSomethingMore(something)
return await doSomethingFinal(more)
} catch (err) {
cleanupEverything()
throw err
}
}
That alone would be worth the price of admission. But wait, there’s more! Not only does the new async function support clean up of simple procedural code, it also simplifies asynchronous loops:
async function awaitMapSeries(items, asyncModifier) {
const out = new Array(items.length)
for (let i = 0; i < items.length; ++i) {
out[i] = await asyncModifier(items[i], i, items)
}
return out
}
async function awaitMapParallel(items, asyncModifier) {
return await Promise.all(items.map(asyncModifier))
}
Awesome, right?
Async Hooks API enables monitoring across Node’s asynchronous operations
Last but not least is Async Hooks, which enables users to monitor and track the state across Node’s asynchronous operations, laying the groundwork for better diagnostic tools in the future. Async Hooks support was added late in the Node 8 development cycle, which was a key factor in our decision to ship New Relic’s initial support for Node 8 without support for async/await. We wanted to leverage Async Hooks to implement our instrumentation support for async/await, but needed additional time to monitor its maturation and to adequately test and validate a solution built on it.
The new improvements and feature releases certainly create the most seamless workflow ever for Node.js developers, likely resulting in significant performance enhancements. For more information on New Relic’s Async/Await support, see the New Relic Node.js documentation.
이 블로그에 표현된 견해는 저자의 견해이며 반드시 New Relic의 견해를 반영하는 것은 아닙니다. 저자가 제공하는 모든 솔루션은 환경에 따라 다르며 New Relic에서 제공하는 상용 솔루션이나 지원의 일부가 아닙니다. 이 블로그 게시물과 관련된 질문 및 지원이 필요한 경우 Explorers Hub(discuss.newrelic.com)에서만 참여하십시오. 이 블로그에는 타사 사이트의 콘텐츠에 대한 링크가 포함될 수 있습니다. 이러한 링크를 제공함으로써 New Relic은 해당 사이트에서 사용할 수 있는 정보, 보기 또는 제품을 채택, 보증, 승인 또는 보증하지 않습니다.