简体   繁体   中英

Importing conditionally exported functions (js)

In:

workers.js :

const threads = require("worker_threads");

// isMainThread is false when in a worker and
// true in the main thread
if (threads.isMainThread) {
  module.exports = function reticulateSplines(splines) {
    return new Promise((resolve, reject) => {
      let reticulator = new threads.Worker(__filename);
      reticulator.postMessage(splines);

      reticulator.on("message", resolve);
      reticulator.on("error", reject);
    });
  };
} else {
  threads.parentPort.once("message", (splines) => {
    for (let spline of splines) {
      spline.reticulate ? spline.reticulate() : (spline.reticulated = true);
    }
    threads.parentPort.postMessage(splines);
  });
}

how can the exported ``reticulateSplines() be imported in another module, eg: runner.js`

import { reticulateSplines } from "./workers";

function reticulate() {
  for (let i = 1; i < 100; ++i);
}
const splines = [
  {
    reticulate: reticulate,
    reticulated: false,
  },
  {
    reticulate: reticulate,
    reticulated: false,
  },
];

reticulateSplines(splines).then(() => {
  console.log("Done reticulating");
});

produces:

import { reticulateSplines } from "./16.24.Worker.Threads";

SyntaxError: The requested module 'file:workers.js' does not provide an export named 'reticulateSplines'        
    at Object.<anonymous> (runner.js:1)
    at Generator.next (<anonymous>)
    at node:internal/main/run_main_module:17:47

You have given module.exports an object, function reticulateSplines() , when you import it, it supposed to be a default import, that is:

import reticulateSplines from "./workers";

Providing more details.....

You can use

exports.reticulateSplines = function (splines) {
    return new Promise((resolve, reject) => {
      let reticulator = new threads.Worker(__filename);
      reticulator.postMessage(splines);

      reticulator.on("message", resolve);
      reticulator.on("error", reject);
    });
  };
} else {
  threads.parentPort.once("message", (splines) => {
    for (let spline of splines) {
      spline.reticulate ? spline.reticulate() : (spline.reticulated = true);
    }
    threads.parentPort.postMessage(splines);
  });
}

then import as:

import {reticulateSplines} from "./workers";

You can also use

module.exports = {
reticulateSplines: function (splines) {
    return new Promise((resolve, reject) => {
      let reticulator = new threads.Worker(__filename);
      reticulator.postMessage(splines);

      reticulator.on("message", resolve);
      reticulator.on("error", reject);
    });
  };
} else {
  threads.parentPort.once("message", (splines) => {
    for (let spline of splines) {
      spline.reticulate ? spline.reticulate() : (spline.reticulated = true);
    }
    threads.parentPort.postMessage(splines);
  });
}} 

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM