diff options
author | Gus Caplan <me@gus.host> | 2018-01-26 09:38:31 -0600 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-02-01 09:33:05 +0100 |
commit | edbcf7c8442d96aabeb564ba1121dad2ff4321a0 (patch) | |
tree | 994c471689a33aac08a1565c23c14123c412dde3 /lib | |
parent | 3cd7977a425d4ef6d2e9b6d535763bc5633789bf (diff) | |
download | android-node-v8-edbcf7c8442d96aabeb564ba1121dad2ff4321a0.tar.gz android-node-v8-edbcf7c8442d96aabeb564ba1121dad2ff4321a0.tar.bz2 android-node-v8-edbcf7c8442d96aabeb564ba1121dad2ff4321a0.zip |
src, lib: return promises from link
Returns the promises created by link so that they can be awaited to get
rid of race conditions while resolving and loading modules.
PR-URL: https://github.com/nodejs/node/pull/18394
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/loader/ModuleJob.js | 24 |
1 files changed, 10 insertions, 14 deletions
diff --git a/lib/internal/loader/ModuleJob.js b/lib/internal/loader/ModuleJob.js index 2d6325b85c..db37765b20 100644 --- a/lib/internal/loader/ModuleJob.js +++ b/lib/internal/loader/ModuleJob.js @@ -6,9 +6,6 @@ const { decorateErrorStack } = require('internal/util'); const assert = require('assert'); const resolvedPromise = SafePromise.resolve(); -const enableDebug = (process.env.NODE_DEBUG || '').match(/\besm\b/) || - process.features.debug; - /* A ModuleJob tracks the loading of a single Module, and the ModuleJobs of * its dependencies, over time. */ class ModuleJob { @@ -27,7 +24,6 @@ class ModuleJob { // Wait for the ModuleWrap instance being linked with all dependencies. const link = async () => { - const dependencyJobs = []; ({ module: this.module, reflect: this.reflect } = await this.modulePromise); if (inspectBrk) { @@ -35,17 +31,17 @@ class ModuleJob { initWrapper(this.module.instantiate, this.module); } assert(this.module instanceof ModuleWrap); - this.module.link(async (dependencySpecifier) => { - const dependencyJobPromise = - this.loader.getModuleJob(dependencySpecifier, url); - dependencyJobs.push(dependencyJobPromise); - const dependencyJob = await dependencyJobPromise; - return (await dependencyJob.modulePromise).module; + + const dependencyJobs = []; + const promises = this.module.link(async (specifier) => { + const jobPromise = this.loader.getModuleJob(specifier, url); + dependencyJobs.push(jobPromise); + return (await (await jobPromise).modulePromise).module; }); - if (enableDebug) { - // Make sure all dependencies are entered into the list synchronously. - Object.freeze(dependencyJobs); - } + + if (promises !== undefined) + await SafePromise.all(promises); + return SafePromise.all(dependencyJobs); }; // Promise for the list of all dependencyJobs. |