1

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.

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
  • 1
    Dynamic import returns a _promise_ of the module. Read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import. – jonrsharpe Jun 15 '22 at 18:05
  • 2
    `import('../path/to/a')` is a dynamic import and it returns a promise itself. I'd suggest just sticking with the static import `import a from '../path/to/a'` and go with that. A couple more lines of code, but simpler code and static imports are better than dynamic imports when possible. – jfriend00 Jun 15 '22 at 18:07

0 Answers0