在node.js es6中如何在一行中导入和运行模块?
遇到一个转换为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')不是函数
研究
- react and Flow:导入节点类型和一行中的列表
- foffeescript:如何在一行中导入Express模块
- 如何在节点中使用导入的ES6模块运行文件
- 如何从节点模块中的命令行运行函数
- 在输入中的导入上运行工厂功能?
- 运行函数
有没有办法在一行中导入和运行模块,因为那将是我唯一需要<代码> 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
- react and flow: import Node type and PureComponent in one line
- Coffeescript: How to import express module in one line
- How to run a file with imported ES6 modules in Node
- How to run functions from Command Line in Node module
- Run factory function on import in TypeScript?
- Run function in script from command line (Node JS)
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论