Skip to content

Instantly share code, notes, and snippets.

@wentout
Last active December 15, 2019 06:55
Show Gist options
  • Save wentout/e048b59621ae875a65e287b0b7433623 to your computer and use it in GitHub Desktop.
Save wentout/e048b59621ae875a65e287b0b7433623 to your computer and use it in GitHub Desktop.
Some thoughts about async storage context for PR 26540
"use strict";
const common = require("../common");
const assert = require("assert");
const { AsyncContext } = require("async_hooks");
const asyncContext = new AsyncContext();
const tasks = [];
asyncContext.enter(store => {
store.set("hello", "world");
tasks.push(() => {
setTimeout(() => {
assert.strictEqual(asyncContext.getStore().get("hello"), "world");
}, 200);
});
});
asyncContext.enter(store => {
store.set("hello", "earth");
tasks.push(() => {
setTimeout(() => {
assert.strictEqual(asyncContext.getStore().get("hello"), "earth");
}, 200);
});
});
process.once('uncaughtException', () => {
assert.strictEqual(asyncContext.getStore().get("hello"), "uncaughtException");
});
asyncContext.enter(store => {
store.set("hello", "uncaughtException");
tasks.push(() => {
setTimeout(() => {
try {
throw new Error('test');
} catch (err) {
process.nextTick(() => {
throw err;
});
}
}, 200);
});
});
setTimeout(() => {
asyncContext.enter(store => {
store.set("hello", "moon");
setTimeout(() => {
assert.strictEqual(asyncContext.getStore().get("hello"), "moon");
setTimeout(() => {
tasks.forEach(task => {
task();
});
}, 100);
}, 200);
});
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment