Javascript/Typescript导出默认Const作为异步函数调用中的值 [英] Javascript/Typescript Export Default Const as value from async function call

查看:61
本文介绍了Javascript/Typescript导出默认Const作为异步函数调用中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多书,但是没有找到可行的解决方案

i've done a bunch of reading but haven't found a working solution

我所看到的最接近的位置是:导出结果React中的异步功能

the closest i've seen is here: Export the result of async function in React

请记住,我要导出一个对象,并且该对象是异步函数的结果,而不是导出异步函数定义本身

please keep in mind that I want to export an object, and that object is the result of an asynchronous function, NOT export the async function definition itself

这是到目前为止我的用例和实现:

here's my use case and implementation so far:

  1. 我们有一个名为config.ts的文件

  1. we have a file called config.ts

传统上config.ts包含一个具有相关运行时配置的对象作为默认导出

traditionally config.ts has contained an object with relevant runtime configurations as the default export

这使我们可以简单地从'../config'或其他任何内容中导入配置

this lets us simply import config from '../config' or whatever

我们的配置和秘密管理变得更加复杂,因此它需要对各种秘密存储库(aw,azure等)进行各种调用

our config and secret management has gotten more complex so that it needs to make various calls to various secret repositories (aws, azure, etc)

我已经将config.ts重构为现在的样子:

i've refactored config.ts to now look something like this:

export const basicValues = {
    color1: 'red'
}

async function buildConfig(){
    const valuesOut = {...basicValues}
    valuesOut.color2 = await getSecret('color2');
    valuesOut.color3 = await getSecret('color3');

    return valuesOut;
}

export default buildConfig()

其中 getSecret 是一些可以进行任意异步调用的函数

where getSecret is some function that makes an arbitrary async call

我正在导出上述basicValues,因为我在其中具有一些配置设置,这些配置设置对于在getSecret内部进行调用是必需的.

i'm exporting basicValues above because i have some config settings in there that are necessary to make the calls inside getSecret.

通过这样导出basicValues,我可以使用简单的 const basicConfig = require('../config').basicValues 来获取值.这样,我们就可以继续在一个干净的,集中的,经过测试的文件中管理所有有趣的配置内容,但仍然可以使这些值很早就可以使用,并且避免了周期性的依赖关系

by exporting basicValues like this i can get the values out with a simple const basicConfig = require('../config').basicValues. This way we can continue to manage all the fun config stuff in a clean, centralized, tested file but still make the values available for use very early and avoid cyclical dependencies

在一起,这种感觉应该起作用

all together, this FEELS like it should work

我已经尝试了许多其他模式,但这听起来最自然,最直观

i've tried lots of other patterns but this feels most natural and intuitive to read

这是不好的部分:

  1. 从'../config'导入配置会产生未定义的内容,并带有 export default buildConfig()
  2. 将导出更改为简单的 export default basicValues 使我们的配置对象符合预期(但显然没有填充异步值)
  1. import config from '../config' yields undefined, with export default buildConfig()
  2. changing the export to be simply export default basicValuesgives us our config object as expected (but without the async values populated, obviously)

我在这里到底在做什么错?

what exactly am I doing wrong here?

很乐意根据需要提供更多的设计

happy to provide more deets as necessary

预先感谢

推荐答案

请记住,我要导出一个对象,并且该对象是异步函数的结果,而不是导出异步函数定义本身

please keep in mind that I want to export an object, and that object is the result of an asynchronous function, NOT export the async function definition itself

这是不可能的.由于该值是异步检索的,因此所有使用该值的模块都必须等待异步操作首先完成-这将需要导出一个Promise,该Promise可以解析为所需的值.

This is not possible. Since the value is retrieved asynchronously, all modules that consume the value must wait for the asynchronous action to complete first - this will require exporting a Promise that resolves to the value you want.

在新版本的Node中,您可以导入Promise并使用顶级 await 来等待其填充:

In new versions of Node, you could import the Promise and use top-level await to wait for it to be populated:

import buildConfigProm from '../config';
const config = await buildConfigProm;

如果您不在Node中,则不支持顶级 await .您可以在Promise导入的任何地方调用 .then :

If you aren't in Node, top-level await isn't supported. You could call .then on the Promise everywhere it's imported:

buildConfigProm.then((config) => {
  // put all the module's code in here
});

如果您不喜欢这样做,唯一的替代方法是使用依赖项注入.让您的模块导出以 config 作为参数的功能,例如:

If you don't like that, the only real alternative is to use dependency injection. Have your modules export functions that take the config as a parameter, eg:

// useConfig.ts
export default (config: Config) => {
  console.log('color2', config.color2);
};

这样,唯一必须异步的是入口点,它等待Promise解析,然后用它调用所需的模块:

This way, the only thing that has to be asynchronous is the entry point, which waits for the Promise to resolve, then calls the needed modules with it:

// index.ts

import buildConfigProm from './config';
import useConfig from './useConfig';
buildConfigProm
  .then((config) => {
    useConfig(config);
  })
  .catch(handleErrors);

这篇关于Javascript/Typescript导出默认Const作为异步函数调用中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆