简体   繁体   English

从 ES6 模块中的 IIFE 返回解构的 object

[英]Return destructured object from IIFE in ES6 module

I can't figure out how to essentially do this at the end here我无法弄清楚如何在最后做到这一点

export {inMemoryDb, backup };

So I want to export the returned value from the IIFE basically as named exports.所以我想基本上将 IIFE 的返回值导出为命名导出。

InMemoryDB.ts InMemoryDB.ts

const result = (async () => {
  const inMemoryDb = newDb();
  await inMemoryDb.public.migrate();
  const backup = inMemoryDb.backup();

  return { inMemoryDb, backup };
})();

export result; 
// but I actually want to export it as { inMemoryDb, backup }

I think it should be我认为应该是

const result = (() => {
    const inMemoryDb = newDb();
    inMemoryDb.public.migrate()
    const backup = inMemoryDb.backup();

    return { inMemoryDb, backup };
}());  //<-- note here.

then然后

export const inMemoryDb = result.inMemoryDb;
export const backup = result.backup

All exports have to be static.所有出口必须是 static。 That allows many opimisations in the module resolution.这允许在模块分辨率中进行许多优化。 Because of that, even if you have an object, you can't export its properties as named exports without explicitly specifying them.因此,即使您有 object,您也无法在未明确指定的情况下将其属性导出为命名导出。


However, the main problem of your code is asynchrony.但是,您的代码的主要问题是异步。 That's what prevents you from just exporting those things as-is, and causes complexities importing it.这就是阻止您按原样导出这些内容并导致导入它的复杂性的原因。

Solution options:解决方案选项:

  1. The Top-level await proposal (Stage 3)顶层await提案(第 3 阶段)

    If you use a platform that supports it, transpile your code using a tool like Babel , or use TypeScript 3.8 or later, then you can already use this feature.如果您使用支持它的平台,使用Babel之类的工具编译代码,或者使用TypeScript 3.8或更高版本,那么您已经可以使用此功能。

    It lets you trash the IIAFE, so you can simply write:它可以让你丢弃 IIAFE,所以你可以简单地写:

     export const inMemoryDb = newDb(); await inMemoryDb.public.migrate(); export const backup = inMemoryDb.backup();

    The best thing about this is that you don't even have to await it when you import it, it's done automatically.最好的一点是,当您import它时,您甚至不必await它,它会自动完成。

    This is the solution to this problem, but for those who don't have access to this feature yet, here are some workarounds:这是此问题解决方案,但对于那些还没有访问此功能的人,这里有一些解决方法:

  2. Exporting everything in a Promise导出 Promise的所有内容

    Well, that doesn't answer your question, as it doesn't use named exports.好吧,这并不能回答您的问题,因为它不使用命名导出。 By the way, that's almost what your code does!顺便说一句,这几乎就是您的代码所做的!

     const result = (async () => { const inMemoryDb = newDb(); await inMemoryDb.public.migrate(); const backup = inMemoryDb.backup(); return { inMemoryDb, backup }; })(); export default result;

    For this to work, your importer will need to:为此,您的进口商需要:

     import result from './inMemoryDB.ts' (async () => { const { inMemoryDb, backup } = await result // Do stuff })()
  3. Exporting everything in separate promises在单独的 Promise 中导出所有内容

     const result = (async () => { const inMemoryDb = newDb(); await inMemoryDb.public.migrate(); const backup = inMemoryDb.backup(); return { inMemoryDb, backup }; })(); export const inMemoryDb = result.then(({inMemoryDb}) => inMemoryDb); export const backup = result.then(({backup}) => backup);

    Importer code:进口商代码:

     import { inMemoryDb, backup } from './inMemoryDB.ts' (async () => { const foo = await inMemoryDb const bar = await backup // Do stuff })()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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