Skip to content

Commit

Permalink
fixup! README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed May 6, 2023
1 parent 612676c commit 6a6be2f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,50 +185,50 @@ only access the value associated with an `AsyncLocal` instance if you have
access to that instance.

```typescript
const asyncLocal = new AsyncLocal();
const local = new AsyncLocal();

// Sets the current value to 'top', and executes the `main` function.
asyncLocal.run("top", main);
local.run("top", main);

function main() {
// AsyncLocal is maintained through other platform queueing.
setTimeout(() => {
console.log(asyncLocal.get()); // => 'top'
console.log(local.get()); // => 'top'

asyncLocal.run("A", () => {
console.log(asyncLocal.get()); // => 'A'
local.run("A", () => {
console.log(local.get()); // => 'A'

setTimeout(() => {
console.log(asyncLocal.get()); // => 'A'
console.log(local.get()); // => 'A'
}, randomTimeout());
});
}, randomTimeout());

// AsyncLocal runs can be nested.
asyncLocal.run("B", () => {
console.log(asyncLocal.get()); // => 'B'
local.run("B", () => {
console.log(local.get()); // => 'B'

setTimeout(() => {
console.log(asyncLocal.get()); // => 'B'
console.log(local.get()); // => 'B'
}, randomTimeout());
});

// AsyncLocal was restored after the previous run.
console.log(asyncLocal.get()); // => 'top'
console.log(local.get()); // => 'top'

// Captures the state of all AsyncLocal's at this moment.
const snapshotDuringTop = new AsyncSnapshot();

asyncLocal.run("C", () => {
console.log(asyncLocal.get()); // => 'C'
local.run("C", () => {
console.log(local.get()); // => 'C'

// The snapshotDuringTop will restore all AsyncLocal to their snapshot
// state and invoke the wrapped function. We pass a function which it will
// invoke.
snapshotDuringTop.run(() => {
// Despite being lexically nested inside 'C', the snapshot restored us to
// to the 'top' state.
console.log(asyncLocal.get()); // => 'top'
console.log(local.get()); // => 'top'
});
});
}
Expand Down Expand Up @@ -312,20 +312,20 @@ tracing span doesn't need to be manually passing around by usercodes.
```typescript
// tracer.js

const asyncLocal = new AsyncLocal();
const local = new AsyncLocal();
export function run(cb) {
// (a)
const span = {
startTime: Date.now(),
traceId: randomUUID(),
spanId: randomUUID(),
};
asyncLocal.run(span, cb);
local.run(span, cb);
}

export function end() {
// (b)
const span = asyncLocal.get();
const span = local.get();
span?.endTime = Date.now();
}
```
Expand Down Expand Up @@ -367,14 +367,14 @@ scheduled with the same priority.

```typescript
const scheduler = {
asyncLocal: new AsyncLocal(),
local: new AsyncLocal(),
postTask(task, options) {
// In practice, the task execution may be deferred.
// Here we simply run the task immediately.
return this.asyncLocal.run({ priority: options.priority }, task);
return this.local.run({ priority: options.priority }, task);
},
currentTask() {
return this.asyncLocal.get() ?? { priority: "default" };
return this.local.get() ?? { priority: "default" };
},
};

Expand Down
28 changes: 14 additions & 14 deletions SCOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ value inside an `AsyncLocal` without explicit access to the `AsyncLocal` instanc
itself.

```typescript
const asyncLocal = new AsyncLocal();
const local = new AsyncLocal();

asyncLocal.run(1, f);
console.log(asyncLocal.get()); // => undefined
local.run(1, f);
console.log(local.get()); // => undefined

function g() {
console.log(asyncLocal.get()); // => 1
console.log(local.get()); // => 1
}

function f() {
// Intentionally named the same "asyncLocal"
const asyncLocal = new AsyncLocal();
asyncLocal.run(2, g);
// Intentionally named the same "local"
const local = new AsyncLocal();
local.run(2, g);
}
```

Expand All @@ -50,18 +50,18 @@ ability to change the value of that variable. You must have direct access to it
in order to affect it.

```typescript
const asyncLocal = new AsyncLocal();
const local = new AsyncLocal();

asyncLocal.run(1, f);
local.run(1, f);

console.log(asyncLocal.get()); // => undefined;
console.log(local.get()); // => undefined;

function f() {
const asyncLocal = new AsyncLocal();
asyncLocal.run(2, g);
const local = new AsyncLocal();
local.run(2, g);

function g() {
console.log(asyncLocal.get()); // => 2;
console.log(local.get()); // => 2;
}
}
```
Expand Down Expand Up @@ -119,5 +119,5 @@ that can operate across sync/async execution should be no different.
There are no differences regarding naming scope of `AsyncLocal` compared to
regular JavaScript variables. Only code with direct access to `AsyncLocal`
instances can modify the value, and only for code execution nested inside a new
`asyncLocal.run()`. Further, the capability to modify a local variable which you
`local.run()`. Further, the capability to modify a local variable which you
have direct access to is already possible in sync code execution.

0 comments on commit 6a6be2f

Please sign in to comment.