V
V
VladimirovJS2017-08-27 13:54:56
JavaScript
VladimirovJS, 2017-08-27 13:54:56

Return a value from a promise and export it to use in other modules?

I use dynamic import of modules in the module depending on the conditions. It returns a promise ( 2ality.com/2017/01/import-operator.html) how can I export the result from the promise from here without creating a global variable?
so of course it doesn't work.

if (условие) {
         import('./mymodule')
        .then(module => {

            if (module.value) {
               const res = module.value
                return res;
            }

        })

  } else {
          import('./mymodule2')
        .then(module => {

            if (module.value2) {
                const res = module.value2
                return res;
            }

        })
}

        export default res;

i need the result in res

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Shashenkov, 2017-08-27
@teknik2008

What's the problem with using it require?

Like this for example
function moduleIf(условие){
  if (условие) {
       let  res = require('./mymodule')
      return res;
  } else {
     let  res = require('./mymodule2')
     return res;
  }
}
export default moduleIf;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question