Skip to content

Commit 13313c0

Browse files
committed
Normalize nouns in the document
1 parent ca0ace8 commit 13313c0

File tree

2 files changed

+56
-68
lines changed

2 files changed

+56
-68
lines changed

README.md

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class AsyncLocal<T = any> {
169169
type ValueChangedListener<T> = (newValue: T, prevValue: T);
170170
```
171171
172-
`AsyncLocal.getValue()` returns the current value of the context.
172+
`AsyncLocal.getValue()` returns the current value of the async execution flow.
173173
174174
As the value of AsyncLocal has to be fetched from its own store, i.e. the
175175
AsyncLocal object. From arbitrary execution context in an async execution
@@ -179,31 +179,27 @@ propagates along with an async execution flow.
179179
AsyncLocal propagates values along the logical async execution flow.
180180
181181
```js
182-
const context = new AsyncLocal();
182+
const asyncLocal = new AsyncLocal();
183183

184184
(function main() {
185-
context.setValue('main');
185+
asyncLocal.setValue('main');
186186

187187
setTimeout(() => {
188-
printContext(); // => 'main'
189-
context.setValue('first timer');
188+
console.log(asyncLocal.getValue()); // => 'main'
189+
asyncLocal.setValue('first timer');
190190
setTimeout(() => {
191-
printContext(); // => 'first timer'
191+
console.log(asyncLocal.getValue()); // => 'first timer'
192192
}, 1000);
193193
}, 1000);
194194

195195
setTimeout(() => {
196-
printContext(); // => 'main'
197-
context.setValue('second timer');
196+
console.log(asyncLocal.getValue()); // => 'main'
197+
asyncLocal.setValue('second timer');
198198
setTimeout(() => {
199-
printContext(); // => 'second timer'
199+
console.log(asyncLocal.getValue()); // => 'second timer'
200200
}, 1000);
201201
}, 1000);
202202
})();
203-
204-
function printContext() {
205-
console.log(context.getValue());
206-
}
207203
```
208204
209205
The optional `valueChangedListener` will be called each time the value in the
@@ -219,7 +215,7 @@ since the code where trigger the listener has to explicitly refer to the
219215
instance of AsyncLocal.
220216
221217
```js
222-
const context = new AsyncLocal(
218+
const asyncLocal = new AsyncLocal(
223219
(newValue, prevValue) =>
224220
console.log(`valueChanged: newValue(${newValue}), prevValue(${prevValue})`)
225221
);
@@ -230,16 +226,16 @@ Promise.resolve().then(run);
230226

231227
async function run() {
232228
// (1)
233-
context.setValue('foo');
229+
asyncLocal.setValue('foo');
234230
await sleep(1000);
235-
await next(context);
231+
await next(asyncLocal);
236232
// (3)
237-
context.setValue('quz');
233+
asyncLocal.setValue('quz');
238234
}
239235

240236
async function next() {
241237
// (2)
242-
context.setValue('bar');
238+
asyncLocal.setValue('bar');
243239
await sleep(1000);
244240
}
245241
```
@@ -391,8 +387,8 @@ class AsyncTask {
391387
```
392388
393389
`AsyncTask.runInAsyncScope` calls the provided function with the provided
394-
arguments in the execution context of the async task. This will establish
395-
the context, call the function, and then restore the original async execution
390+
arguments in the async context of the async task. This will establish
391+
the async context, call the function, and then restore the original async
396392
context.
397393
398394
### Using `AsyncTask`
@@ -403,7 +399,7 @@ function connect(port, host) {
403399
let nextId = 0;
404400
const requestResponseMap = new Map();
405401

406-
// Establish the connection, the client is linked to the current context.
402+
// Establish the connection, the client is linked to the current async context.
407403
const client = net.createConnection({ host, port });
408404
client.on('connect', () => {
409405
console.log('connected to server');
@@ -412,9 +408,9 @@ function connect(port, host) {
412408
console.log('disconnected from server');
413409
});
414410

415-
// the client is created at the context of `connect`,
416-
// the listeners will be triggered at the context of the
417-
// client initiating context.
411+
// the client is created at the async context of `connect`,
412+
// the listeners will be triggered at the async context of the
413+
// client initiating async context.
418414
client.on('data', (res) => {
419415
const { id, data } = JSON.parse(res.toString('utf8'));
420416
const req = requestResponseMap.get(id);
@@ -423,7 +419,7 @@ function connect(port, host) {
423419
return;
424420
}
425421

426-
// The req.handler callback is called under the context of client
422+
// The req.handler callback is called under the async context of client
427423
// listeners.
428424
req.handler(data);
429425
});
@@ -439,7 +435,7 @@ function connect(port, host) {
439435
// AsyncTask & Promise based connection wrapper.
440436
class DatabaseConnection {
441437
constructor(port, host) {
442-
// Initialize connection, possibly in root context.
438+
// Initialize connection, possibly in root async context.
443439
this.socket = connect(port, host);
444440
}
445441

@@ -470,13 +466,13 @@ class QueryTask extends AsyncTask {
470466
}
471467
```
472468
473-
In the example above, `DatabaseConnection` can be established at root execution
469+
In the example above, `DatabaseConnection` can be established at root async
474470
context (or any other context). With `AsyncTask`, each call to
475471
`DatabaseConnection.query` will schedule an async task, which will be linked
476-
to its initiator execution context (may not be the one establishing
472+
to its initiator async context (may not be the one establishing
477473
`DatabaseConnection`). And at the resolution of socket, the contexts are
478474
propagated by the `DatabaseConnection`, which is linked to its initiating
479-
execution context, so the context has to be re-established by
475+
async context, so the async context has to be re-established by
480476
`AsyncTask.runInAsyncScope`.
481477
482478
In this way, we can propagate correct async context flows on multiplexing

SOLUTION.md

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,66 +8,58 @@ propagated along with the async execution flow.
88
We'll still have the example from [README.md][] for a introduction.
99

1010
```js
11-
const context = new AsyncLocal();
11+
const asyncLocal = new AsyncLocal();
1212

1313
(function main() {
14-
context.setValue('main');
14+
asyncLocal.setValue('main');
1515

1616
// (1)
1717
setTimeout(() => {
18-
printContext(); // => 'main'
19-
context.setValue('first timer');
18+
console.log(asyncLocal.getValue()); // => 'main'
19+
asyncLocal.setValue('first timer');
2020
setTimeout(() => {
21-
printContext(); // => 'first timer'
21+
console.log(asyncLocal.getValue()); // => 'first timer'
2222
}, 1000);
2323
}, 1000);
2424

2525
// (2)
2626
setTimeout(() => {
27-
printContext(); // => 'main'
28-
context.setValue('second timer');
27+
console.log(asyncLocal.getValue()); // => 'main'
28+
asyncLocal.setValue('second timer');
2929
setTimeout(() => {
30-
printContext(); // => 'second timer'
30+
console.log(asyncLocal.getValue()); // => 'second timer'
3131
}, 1000);
3232
}, 1000);
3333
})();
34-
35-
function printContext() {
36-
console.log(context.getValue());
37-
}
3834
```
3935

4036
In the example above, the `main` function first filled the AsyncLocal with
41-
value `'main'` by `context.setValue('main')`, then it called `setTimeout` twice
37+
value `'main'` by `asyncLocal.setValue('main')`, then it called `setTimeout` twice
4238
consecutively. In the callback of `setTimeout`, first the value of AsyncLocal
4339
were printed, then each callback of `setTimeout` set AsyncLocal with different
4440
value `'first time'` and `'second time'` respectively. After the value set,
4541
both callback of `setTimeout` initiated with a new `setTimeout` with a callback
4642
to print the value of AsyncLocal.
4743

48-
The notable things are that we have `context.setValue` been placed in a nested
44+
The notable things are that we have `asyncLocal.setValue` been placed in a nested
4945
callback of `setTimeout`, an asynchronous API provided by many outstanding
5046
hosts like web browsers and Node.js. Why is this necessary? Can we replace
5147
the `setTimeout` with an async function and `await` them? Like following
5248
snippet:
5349

5450
```js
55-
const context = new AsyncLocal();
51+
const asyncLocal = new AsyncLocal();
5652

5753
(async function main() {
58-
context.setValue(0);
54+
asyncLocal.setValue(0);
5955

6056
await Promise.all([ test(), test() ]);
61-
printContext();
57+
console.log(asyncLocal.getValue());
6258
})();
6359

6460
async function test() {
65-
printContext();
66-
context.setValue(context.getValue() + 1);
67-
}
68-
69-
function printContext() {
70-
console.log(context.getValue());
61+
console.log(asyncLocal.getValue());
62+
asyncLocal.setValue(asyncLocal.getValue() + 1);
7163
}
7264
```
7365

@@ -78,20 +70,20 @@ calling an async function as they can be desugared as following:
7870
```js
7971
function main() {
8072
return new Promise((resolve, reject) => {
81-
context.setValue(0);
73+
asyncLocal.setValue(0);
8274
var result = Promise.all([ test(), test() ]);
8375
Promise.resolve(result)
8476
.then(() => {
85-
printContext();
77+
console.log(asyncLocal.getValue());
8678
resolve(undefined);
8779
});
8880
});
8981
}
9082

9183
function test() {
9284
return new Promise((resolve, reject) => {
93-
printContext();
94-
context.setValue(context.getValue() + 1);
85+
console.log(asyncLocal.getValue());
86+
asyncLocal.setValue(asyncLocal.getValue() + 1);
9587
resolve(undefined);
9688
});
9789
}
@@ -100,9 +92,9 @@ function test() {
10092
The output of snippet above will be:
10193

10294
```log
103-
0 // test: printContext();
104-
1 // test: printContext();
105-
2 // main: printContext();
95+
0 // test: console.log(asyncLocal.getValue());
96+
1 // test: console.log(asyncLocal.getValue());
97+
2 // main: console.log(asyncLocal.getValue());
10698
```
10799

108100
> For more async function evaluation definition, checkout [25.7.5.1 AsyncFunctionStart][].
@@ -115,19 +107,19 @@ For that reason, normal recursive async function will not be punished on
115107
performance.
116108

117109
```js
118-
const context = new AsyncLocal();
110+
const asyncLocal = new AsyncLocal();
119111

120112
(async function main() {
121-
context.setValue(0);
113+
asyncLocal.setValue(0);
122114

123115
await test();
124-
console.log(context.getValue());
116+
console.log(asyncLocal.getValue());
125117
})();
126118

127119
async function test() {
128-
const value = context.getValue();
120+
const value = asyncLocal.getValue();
129121
if (value < 100) {
130-
context.setValue(value + 1);
122+
asyncLocal.setValue(value + 1);
131123
await test();
132124
};
133125
}
@@ -143,14 +135,14 @@ the AsyncLocal, it is notable that the semantics for propagation of the value
143135
is assignment but not deep copy.
144136

145137
```js
146-
const context = new AsyncLocal();
138+
const asyncLocal = new AsyncLocal();
147139

148-
context.setValue({});
140+
asyncLocal.setValue({});
149141
setTimeout(() => {
150-
const ctx = context.getValue();
142+
const ctx = asyncLocal.getValue();
151143
ctx.foo = 'bar';
152144
setTimeout(() => {
153-
console.log(context.getValue()); // => { foo: 'bar' };
145+
console.log(asyncLocal.getValue()); // => { foo: 'bar' };
154146
}, 1000);
155147
}, 1000);
156148
```

0 commit comments

Comments
 (0)