Skip to content

Commit ccbf4f9

Browse files
puzpuzpuztargos
authored andcommitted
async_hooks: merge run and exit methods
PR-URL: nodejs#31950 Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
1 parent e61bcd2 commit ccbf4f9

12 files changed

+29
-214
lines changed

benchmark/async_hooks/async-resource-vs-destroy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function buildDestroy(getServe) {
103103
function buildAsyncLocalStorage(getServe) {
104104
const asyncLocalStorage = new AsyncLocalStorage();
105105
const server = createServer((req, res) => {
106-
asyncLocalStorage.runSyncAndReturn({}, () => {
106+
asyncLocalStorage.run({}, () => {
107107
getServe(getCLS, setCLS)(req, res);
108108
});
109109
});

doc/api/async_hooks.md

+16-130
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ added: REPLACEME
922922
-->
923923

924924
Creates a new instance of `AsyncLocalStorage`. Store is only provided within a
925-
`run` or a `runSyncAndReturn` method call.
925+
`run` method call.
926926

927927
### `asyncLocalStorage.disable()`
928928
<!-- YAML
@@ -931,8 +931,7 @@ added: REPLACEME
931931

932932
This method disables the instance of `AsyncLocalStorage`. All subsequent calls
933933
to `asyncLocalStorage.getStore()` will return `undefined` until
934-
`asyncLocalStorage.run()` or `asyncLocalStorage.runSyncAndReturn()`
935-
is called again.
934+
`asyncLocalStorage.run()` is called again.
936935

937936
When calling `asyncLocalStorage.disable()`, all current contexts linked to the
938937
instance will be exited.
@@ -954,8 +953,7 @@ added: REPLACEME
954953

955954
This method returns the current store.
956955
If this method is called outside of an asynchronous context initialized by
957-
calling `asyncLocalStorage.run` or `asyncLocalStorage.runAndReturn`, it will
958-
return `undefined`.
956+
calling `asyncLocalStorage.run`, it will return `undefined`.
959957

960958
### `asyncLocalStorage.enterWith(store)`
961959
<!-- YAML
@@ -1008,90 +1006,23 @@ added: REPLACEME
10081006
* `callback` {Function}
10091007
* `...args` {any}
10101008

1011-
Calling `asyncLocalStorage.run(callback)` will create a new asynchronous
1012-
context. Within the callback function and the asynchronous operations from
1013-
the callback, `asyncLocalStorage.getStore()` will return the object or
1014-
the primitive value passed into the `store` argument (known as "the store").
1015-
This store will be persistent through the following asynchronous calls.
1016-
1017-
The callback will be ran asynchronously. Optionally, arguments can be passed
1018-
to the function. They will be passed to the callback function.
1019-
1020-
If an error is thrown by the callback function, it will not be caught by
1021-
a `try/catch` block as the callback is ran in a new asynchronous resource.
1022-
Also, the stacktrace will be impacted by the asynchronous call.
1023-
1024-
Example:
1025-
1026-
```js
1027-
const store = { id: 1 };
1028-
asyncLocalStorage.run(store, () => {
1029-
asyncLocalStorage.getStore(); // Returns the store object
1030-
someAsyncOperation(() => {
1031-
asyncLocalStorage.getStore(); // Returns the same object
1032-
});
1033-
});
1034-
asyncLocalStorage.getStore(); // Returns undefined
1035-
```
1036-
1037-
### `asyncLocalStorage.exit(callback[, ...args])`
1038-
<!-- YAML
1039-
added: REPLACEME
1040-
-->
1041-
1042-
* `callback` {Function}
1043-
* `...args` {any}
1044-
1045-
Calling `asyncLocalStorage.exit(callback)` will create a new asynchronous
1046-
context.
1047-
Within the callback function and the asynchronous operations from the callback,
1048-
`asyncLocalStorage.getStore()` will return `undefined`.
1049-
1050-
The callback will be ran asynchronously. Optionally, arguments can be passed
1051-
to the function. They will be passed to the callback function.
1052-
1053-
If an error is thrown by the callback function, it will not be caught by
1054-
a `try/catch` block as the callback is ran in a new asynchronous resource.
1055-
Also, the stacktrace will be impacted by the asynchronous call.
1056-
1057-
Example:
1058-
1059-
```js
1060-
asyncLocalStorage.run('store value', () => {
1061-
asyncLocalStorage.getStore(); // Returns 'store value'
1062-
asyncLocalStorage.exit(() => {
1063-
asyncLocalStorage.getStore(); // Returns undefined
1064-
});
1065-
asyncLocalStorage.getStore(); // Returns 'store value'
1066-
});
1067-
```
1068-
1069-
### `asyncLocalStorage.runSyncAndReturn(store, callback[, ...args])`
1070-
<!-- YAML
1071-
added: REPLACEME
1072-
-->
1073-
1074-
* `store` {any}
1075-
* `callback` {Function}
1076-
* `...args` {any}
1077-
10781009
This methods runs a function synchronously within a context and return its
10791010
return value. The store is not accessible outside of the callback function or
10801011
the asynchronous operations created within the callback.
10811012

10821013
Optionally, arguments can be passed to the function. They will be passed to
10831014
the callback function.
10841015

1085-
If the callback function throws an error, it will be thrown by
1086-
`runSyncAndReturn` too. The stacktrace will not be impacted by this call and
1087-
the context will be exited.
1016+
If the callback function throws an error, it will be thrown by `run` too.
1017+
The stacktrace will not be impacted by this call and the context will
1018+
be exited.
10881019

10891020
Example:
10901021

10911022
```js
10921023
const store = { id: 2 };
10931024
try {
1094-
asyncLocalStorage.runSyncAndReturn(store, () => {
1025+
asyncLocalStorage.run(store, () => {
10951026
asyncLocalStorage.getStore(); // Returns the store object
10961027
throw new Error();
10971028
});
@@ -1101,7 +1032,7 @@ try {
11011032
}
11021033
```
11031034

1104-
### `asyncLocalStorage.exitSyncAndReturn(callback[, ...args])`
1035+
### `asyncLocalStorage.exit(callback[, ...args])`
11051036
<!-- YAML
11061037
added: REPLACEME
11071038
-->
@@ -1116,17 +1047,17 @@ the asynchronous operations created within the callback.
11161047
Optionally, arguments can be passed to the function. They will be passed to
11171048
the callback function.
11181049

1119-
If the callback function throws an error, it will be thrown by
1120-
`exitSyncAndReturn` too. The stacktrace will not be impacted by this call and
1050+
If the callback function throws an error, it will be thrown by `exit` too.
1051+
The stacktrace will not be impacted by this call and
11211052
the context will be re-entered.
11221053

11231054
Example:
11241055

11251056
```js
1126-
// Within a call to run or runSyncAndReturn
1057+
// Within a call to run
11271058
try {
11281059
asyncLocalStorage.getStore(); // Returns the store object or value
1129-
asyncLocalStorage.exitSyncAndReturn(() => {
1060+
asyncLocalStorage.exit(() => {
11301061
asyncLocalStorage.getStore(); // Returns undefined
11311062
throw new Error();
11321063
});
@@ -1136,68 +1067,23 @@ try {
11361067
}
11371068
```
11381069

1139-
### Choosing between `run` and `runSyncAndReturn`
1140-
1141-
#### When to choose `run`
1142-
1143-
`run` is asynchronous. It is called with a callback function that
1144-
runs within a new asynchronous call. This is the most explicit behavior as
1145-
everything that is executed within the callback of `run` (including further
1146-
asynchronous operations) will have access to the store.
1147-
1148-
If an instance of `AsyncLocalStorage` is used for error management (for
1149-
instance, with `process.setUncaughtExceptionCaptureCallback`), only
1150-
exceptions thrown in the scope of the callback function will be associated
1151-
with the context.
1152-
1153-
This method is the safest as it provides strong scoping and consistent
1154-
behavior.
1155-
1156-
It cannot be promisified using `util.promisify`. If needed, the `Promise`
1157-
constructor can be used:
1158-
1159-
```js
1160-
const store = new Map(); // initialize the store
1161-
new Promise((resolve, reject) => {
1162-
asyncLocalStorage.run(store, () => {
1163-
someFunction((err, result) => {
1164-
if (err) {
1165-
return reject(err);
1166-
}
1167-
return resolve(result);
1168-
});
1169-
});
1170-
});
1171-
```
1172-
1173-
#### When to choose `runSyncAndReturn`
1174-
1175-
`runSyncAndReturn` is synchronous. The callback function will be executed
1176-
synchronously and its return value will be returned by `runSyncAndReturn`.
1177-
The store will only be accessible from within the callback
1178-
function and the asynchronous operations created within this scope.
1179-
If the callback throws an error, `runSyncAndReturn` will throw it and it will
1180-
not be associated with the context.
1181-
1182-
This method provides good scoping while being synchronous.
1183-
1184-
#### Usage with `async/await`
1070+
### Usage with `async/await`
11851071

11861072
If, within an async function, only one `await` call is to run within a context,
11871073
the following pattern should be used:
11881074

11891075
```js
11901076
async function fn() {
1191-
await asyncLocalStorage.runSyncAndReturn(new Map(), () => {
1077+
await asyncLocalStorage.run(new Map(), () => {
11921078
asyncLocalStorage.getStore().set('key', value);
11931079
return foo(); // The return value of foo will be awaited
11941080
});
11951081
}
11961082
```
11971083

11981084
In this example, the store is only available in the callback function and the
1199-
functions called by `foo`. Outside of `runSyncAndReturn`, calling `getStore`
1200-
will return `undefined`.
1085+
functions called by `foo`. Outside of `run`, calling `getStore` will return
1086+
`undefined`.
12011087

12021088
[`after` callback]: #async_hooks_after_asyncid
12031089
[`before` callback]: #async_hooks_before_asyncid

lib/async_hooks.js

+2-18
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,15 @@ class AsyncLocalStorage {
256256
resource[this.kResourceStore] = store;
257257
}
258258

259-
runSyncAndReturn(store, callback, ...args) {
259+
run(store, callback, ...args) {
260260
const resource = new AsyncResource('AsyncLocalStorage');
261261
return resource.runInAsyncScope(() => {
262262
this.enterWith(store);
263263
return callback(...args);
264264
});
265265
}
266266

267-
exitSyncAndReturn(callback, ...args) {
267+
exit(callback, ...args) {
268268
if (!this.enabled) {
269269
return callback(...args);
270270
}
@@ -282,22 +282,6 @@ class AsyncLocalStorage {
282282
return resource[this.kResourceStore];
283283
}
284284
}
285-
286-
run(store, callback, ...args) {
287-
process.nextTick(() => {
288-
this.enterWith(store);
289-
return callback(...args);
290-
});
291-
}
292-
293-
exit(callback, ...args) {
294-
if (!this.enabled) {
295-
return process.nextTick(callback, ...args);
296-
}
297-
this.enabled = false;
298-
process.nextTick(callback, ...args);
299-
this.enabled = true;
300-
}
301285
}
302286

303287
// Placing all exports down here because the exported classes won't export

test/async-hooks/test-async-local-storage-args.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@ const { AsyncLocalStorage } = require('async_hooks');
66
const asyncLocalStorage = new AsyncLocalStorage();
77

88
asyncLocalStorage.run({}, (runArg) => {
9-
assert.strictEqual(runArg, 1);
10-
asyncLocalStorage.exit((exitArg) => {
11-
assert.strictEqual(exitArg, 2);
12-
}, 2);
13-
}, 1);
14-
15-
asyncLocalStorage.runSyncAndReturn({}, (runArg) => {
169
assert.strictEqual(runArg, 'foo');
17-
asyncLocalStorage.exitSyncAndReturn((exitArg) => {
10+
asyncLocalStorage.exit((exitArg) => {
1811
assert.strictEqual(exitArg, 'bar');
1912
}, 'bar');
2013
}, 'foo');

test/async-hooks/test-async-local-storage-async-await.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function test() {
1212
}
1313

1414
async function main() {
15-
await asyncLocalStorage.runSyncAndReturn(new Map(), test);
15+
await asyncLocalStorage.run(new Map(), test);
1616
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
1717
}
1818

test/async-hooks/test-async-local-storage-async-functions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async function testAwait() {
1616
await foo();
1717
assert.notStrictEqual(asyncLocalStorage.getStore(), undefined);
1818
assert.strictEqual(asyncLocalStorage.getStore().get('key'), 'value');
19-
await asyncLocalStorage.exitSyncAndReturn(testOut);
19+
await asyncLocalStorage.exit(testOut);
2020
}
2121

2222
asyncLocalStorage.run(new Map(), () => {

test/async-hooks/test-async-local-storage-enable-disable.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { AsyncLocalStorage } = require('async_hooks');
55

66
const asyncLocalStorage = new AsyncLocalStorage();
77

8-
asyncLocalStorage.runSyncAndReturn(new Map(), () => {
8+
asyncLocalStorage.run(new Map(), () => {
99
asyncLocalStorage.getStore().set('foo', 'bar');
1010
process.nextTick(() => {
1111
assert.strictEqual(asyncLocalStorage.getStore().get('foo'), 'bar');
@@ -24,7 +24,7 @@ asyncLocalStorage.runSyncAndReturn(new Map(), () => {
2424

2525
process.nextTick(() => {
2626
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
27-
asyncLocalStorage.runSyncAndReturn(new Map(), () => {
27+
asyncLocalStorage.run(new Map(), () => {
2828
assert.notStrictEqual(asyncLocalStorage.getStore(), undefined);
2929
});
3030
});

test/async-hooks/test-async-local-storage-errors-async.js

-26
This file was deleted.

test/async-hooks/test-async-local-storage-errors-sync-ret.js test/async-hooks/test-async-local-storage-errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ process.setUncaughtExceptionCaptureCallback((err) => {
1414
});
1515

1616
try {
17-
asyncLocalStorage.runSyncAndReturn(new Map(), () => {
17+
asyncLocalStorage.run(new Map(), () => {
1818
const store = asyncLocalStorage.getStore();
1919
store.set('hello', 'node');
2020
setTimeout(() => {

test/async-hooks/test-async-local-storage-gcable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const onGC = require('../common/ongc');
1010

1111
let asyncLocalStorage = new AsyncLocalStorage();
1212

13-
asyncLocalStorage.runSyncAndReturn({}, () => {
13+
asyncLocalStorage.run({}, () => {
1414
asyncLocalStorage.disable();
1515

1616
onGC(asyncLocalStorage, { ongc: common.mustCall() });

test/async-hooks/test-async-local-storage-misc-stores.js

+3-12
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,11 @@ const { AsyncLocalStorage } = require('async_hooks');
55

66
const asyncLocalStorage = new AsyncLocalStorage();
77

8-
asyncLocalStorage.run(42, () => {
9-
assert.strictEqual(asyncLocalStorage.getStore(), 42);
8+
asyncLocalStorage.run('hello node', () => {
9+
assert.strictEqual(asyncLocalStorage.getStore(), 'hello node');
1010
});
1111

12-
const runStore = { foo: 'bar' };
12+
const runStore = { hello: 'node' };
1313
asyncLocalStorage.run(runStore, () => {
1414
assert.strictEqual(asyncLocalStorage.getStore(), runStore);
1515
});
16-
17-
asyncLocalStorage.runSyncAndReturn('hello node', () => {
18-
assert.strictEqual(asyncLocalStorage.getStore(), 'hello node');
19-
});
20-
21-
const runSyncStore = { hello: 'node' };
22-
asyncLocalStorage.runSyncAndReturn(runSyncStore, () => {
23-
assert.strictEqual(asyncLocalStorage.getStore(), runSyncStore);
24-
});

0 commit comments

Comments
 (0)