mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
The emit{Before,After} APIs in AsyncResource are problematic.
* emit{Before,After} are named to suggest that the only thing they do
is emit the before and after hooks. However, they in fact, mutate
the current execution context.
* They must be properly nested. Failure to do so by user code leads
to catastrophic (unrecoverable) exceptions. It is very easy for the
users to forget that they must be using a try/finally block around
the code that must be surrounded by these operations. Even the
example provided in the official docs makes this mistake. Failing
to use a finally can lead to a catastrophic crash if the callback
ends up throwing.
This change provides a safer `runInAsyncScope` API as an alternative
and deprecates emit{Before,After}.
PR-URL: https://github.com/nodejs/node/pull/18513
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
12 lines
331 B
JavaScript
12 lines
331 B
JavaScript
'use strict';
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const async_hooks = require('async_hooks');
|
|
|
|
// Ensure that asyncResource.makeCallback returns the callback return value.
|
|
const a = new async_hooks.AsyncResource('foobar');
|
|
const ret = a.runInAsyncScope(() => {
|
|
return 1729;
|
|
});
|
|
assert.strictEqual(ret, 1729);
|