在node.js es6中如何在一行中导入和运行模块?

发布于 2025-02-08 08:41:48 字数 1968 浏览 3 评论 0原文

遇到一个转换为ES6导入的问题,我没有从搜索中找到解决方案,但是传统上,我能够创建一个Promise as:

Promise.all([
  require('../path/to/a')('foo'),
  require('../path/to/b')('bar'),
])
  .then(() => {
    // further code
  })
  .catch(e => {
    console.log(e.message)
  })

切换到ES6,我必须编写我的Promise> Promise> Promise < /code> as:

import a from '../path/to/a'
import b from '../path/to/b'

Promise.all([
  a('foo'),
  b('bar'),
])
  .then(() => {
    // further code
  })
  .catch(e => {
    console.log(e.message)
  })

为了使其工作,但是如果我尝试在一行中导入并运行:

Promise.all([
  import('../path/to/a')('foo'),
  import('../path/to/b')('bar'),
])
  .then(() => {
    // further code
  })
  .catch(e => {
    console.log(e.message)
  })

我得到:

importCall('../ path/to/a')不是函数

研究

有没有办法在一行中导入和运行模块,因为那将是我唯一需要<代码> a()和b()?请注意,我不是在使用打字稿,但是我对解决方案的搜索导致了许多Typescript Q&amp; as。

Running into an issue with converting to ES6 import I haven't found a solution from searches but traditionally I was able to create a Promise as:

Promise.all([
  require('../path/to/a')('foo'),
  require('../path/to/b')('bar'),
])
  .then(() => {
    // further code
  })
  .catch(e => {
    console.log(e.message)
  })

and switching to ES6 I have to write my Promise as:

import a from '../path/to/a'
import b from '../path/to/b'

Promise.all([
  a('foo'),
  b('bar'),
])
  .then(() => {
    // further code
  })
  .catch(e => {
    console.log(e.message)
  })

for it to work but if I try to import and run on one line:

Promise.all([
  import('../path/to/a')('foo'),
  import('../path/to/b')('bar'),
])
  .then(() => {
    // further code
  })
  .catch(e => {
    console.log(e.message)
  })

I get:

ImportCall('../path/to/a') is not a function

Research

Is there a way to import and run a module in one line since that would be the only instance I would need a() and b()? Note I am not using TypeScript but my search for a solution resulted in many TypeScript Q&As.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文