| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ddd339f commit 16aa927
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -727,6 +727,32 @@ class DBQuery extends AsyncResource { | |||
| 727 | 727 | } | |
| 728 | 728 | ``` | |
| 729 | 729 | ||
| 730 | + #### `static AsyncResource.bind(fn[, type])` | ||
| 731 | + <!-- YAML | ||
| 732 | + added: REPLACEME | ||
| 733 | + --> | ||
| 734 | + | ||
| 735 | + * `fn` {Function} The function to bind to the current execution context. | ||
| 736 | + * `type` {string} An optional name to associate with the underlying | ||
| 737 | + `AsyncResource`. | ||
| 738 | + | ||
| 739 | + Binds the given function to the current execution context. | ||
| 740 | + | ||
| 741 | + The returned function will have an `asyncResource` property referencing | ||
| 742 | + the `AsyncResource` to which the function is bound. | ||
| 743 | + | ||
| 744 | + #### `asyncResource.bind(fn)` | ||
| 745 | + <!-- YAML | ||
| 746 | + added: REPLACEME | ||
| 747 | + --> | ||
| 748 | + | ||
| 749 | + * `fn` {Function} The function to bind to the current `AsyncResource`. | ||
| 750 | + | ||
| 751 | + Binds the given function to execute to this `AsyncResource`'s scope. | ||
| 752 | + | ||
| 753 | + The returned function will have an `asyncResource` property referencing | ||
| 754 | + the `AsyncResource` to which the function is bound. | ||
| 755 | + | ||
| 730 | 756 | #### `asyncResource.runInAsyncScope(fn[, thisArg, ...args])` | |
| 731 | 757 | <!-- YAML | |
| 732 | 758 | added: v9.6.0 | |
@@ -898,12 +924,12 @@ const { createServer } = require('http'); | |||
| 898 | 924 | const { AsyncResource, executionAsyncId } = require('async_hooks'); | |
| 899 | 925 | ||
| 900 | 926 | const server = createServer((req, res) => { | |
| 901 | - const asyncResource = new AsyncResource('request'); | ||
| 902 | - // The listener will always run in the execution context of `asyncResource`. | ||
| 903 | - req.on('close', asyncResource.runInAsyncScope.bind(asyncResource, () => { | ||
| 904 | - // Prints: true | ||
| 905 | - console.log(asyncResource.asyncId() === executionAsyncId()); | ||
| 927 | + req.on('close', AsyncResource.bind(() => { | ||
| 928 | + // Execution context is bound to the current outer scope. | ||
| 906 | 929 | })); | |
| 930 | + req.on('close', () => { | ||
| 931 | + // Execution context is bound to the scope that caused 'close' to emit. | ||
| 932 | + }); | ||
| 907 | 933 | res.end(); | |
| 908 | 934 | }).listen(3000); | |
| 909 | 935 | ``` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,13 +2,15 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | 4 | NumberIsSafeInteger, | |
| 5 | + ObjectDefineProperties, | ||
| 5 | 6 | ReflectApply, | |
| 6 | 7 | Symbol, | |
| 7 | 8 | } = primordials; | |
| 8 | 9 | ||
| 9 | 10 | const { | |
| 10 | 11 | ERR_ASYNC_CALLBACK, | |
| 11 | 12 | ERR_ASYNC_TYPE, | |
| 13 | + ERR_INVALID_ARG_TYPE, | ||
| 12 | 14 | ERR_INVALID_ASYNC_ID | |
| 13 | 15 | } = require('internal/errors').codes; | |
| 14 | 16 | const { validateString } = require('internal/validators'); | |
@@ -211,6 +213,28 @@ class AsyncResource { | |||
| 211 | 213 | triggerAsyncId() { | |
| 212 | 214 | return this[trigger_async_id_symbol]; | |
| 213 | 215 | } | |
| 216 | + | ||
| 217 | + bind(fn) { | ||
| 218 | + if (typeof fn !== 'function') | ||
| 219 | + throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn); | ||
| 220 | + const ret = this.runInAsyncScope.bind(this, fn); | ||
| 221 | + ObjectDefineProperties(ret, { | ||
| 222 | + 'length': { | ||
| 223 | + enumerable: true, | ||
| 224 | + value: fn.length, | ||
| 225 | + }, | ||
| 226 | + 'asyncResource': { | ||
| 227 | + enumerable: true, | ||
| 228 | + value: this, | ||
| 229 | + } | ||
| 230 | + }); | ||
| 231 | + return ret; | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + static bind(fn, type) { | ||
| 235 | + type = type || fn.name; | ||
| 236 | + return (new AsyncResource(type || 'bound-anonymous-fn')).bind(fn); | ||
| 237 | + } | ||
| 214 | 238 | } | |
| 215 | 239 | ||
| 216 | 240 | const storageList = []; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,35 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const { AsyncResource, executionAsyncId } = require('async_hooks'); | ||
| 6 | + | ||
| 7 | + const fn = common.mustCall(AsyncResource.bind(() => { | ||
| 8 | + return executionAsyncId(); | ||
| 9 | + })); | ||
| 10 | + | ||
| 11 | + setImmediate(() => { | ||
| 12 | + const asyncId = executionAsyncId(); | ||
| 13 | + assert.notStrictEqual(asyncId, fn()); | ||
| 14 | + }); | ||
| 15 | + | ||
| 16 | + const asyncResource = new AsyncResource('test'); | ||
| 17 | + | ||
| 18 | + [1, false, '', {}, []].forEach((i) => { | ||
| 19 | + assert.throws(() => asyncResource.bind(i), { | ||
| 20 | + code: 'ERR_INVALID_ARG_TYPE' | ||
| 21 | + }); | ||
| 22 | + }); | ||
| 23 | + | ||
| 24 | + const fn2 = asyncResource.bind((a, b) => { | ||
| 25 | + return executionAsyncId(); | ||
| 26 | + }); | ||
| 27 | + | ||
| 28 | + assert.strictEqual(fn2.asyncResource, asyncResource); | ||
| 29 | + assert.strictEqual(fn2.length, 2); | ||
| 30 | + | ||
| 31 | + setImmediate(() => { | ||
| 32 | + const asyncId = executionAsyncId(); | ||
| 33 | + assert.strictEqual(asyncResource.asyncId(), fn2()); | ||
| 34 | + assert.notStrictEqual(asyncId, fn2()); | ||
| 35 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments