承诺完成后出口 [英] exporting after promise finishes

查看:85
本文介绍了承诺完成后出口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在另一个我无法修改的模块中导出一个初始状态依赖于从 Promise 返回的值的类。

I would like to export a class which initial state depends on a value returned from a Promise in another module i cannot modify.

以下是代码:

let e = true;

APromiseFromAnotherModule()
  .then(value => return value;);

export default class E {
  constructor() {
    if (e) {
      //...
    } else {
      //...
    }
  }
}

I还尝试使用 async / await Promise 封装到异步函数中,如下所示:

I also tried with async/await encapsulating the Promise into an async function like this:

let e = true;

getInitialValue = async () => {
  return await APromiseFromAnotherModule()
    .then(value => e = value;);
};

e = getInitialValue();

export default class E {
  constructor() {
    if (e) {
      //...
    } else {
      //...
    }
  }
}

但是它没有意义,因为那是一个 async 函数,所以很明显它不起作用。

But it doesn't make sense because that one is an async function so obviously it doesn't work.

我缺少什么?

推荐答案

模块导出同步完成。因此,它们不能依赖于异步操作的结果。

module exports are done synchronously. So, they cannot depend upon the results of an asynchronous operation.

并且, await 仅适用于函数内部。它实际上并不阻止包含函数(包含函数返回一个promise),因此无法帮助您将异步操作转换为同步操作。

And, await only works inside a function. It doesn't actually block the containing function (the containing function returns a promise) so that won't help you make an async operation into a synchronous one either.

处理在其设置中使用某些异步代码的模块的常用方法是导出promise并让调用代码在promise上使用 .then()或初始化带有构造函数的模块,它返回一个promise。

The usual ways to deal with a module that uses some async code in its setup is to either export a promise and have the calling code use .then() on the promise or to initialize the module with a constructor function that returns a promise.

代码只是伪代码,所以很难准确地告诉你真正的问题是为你展示特定代码情况。

The code is only pseudo code so it's hard to tell exactly what your real problem is to show you specific code for your situation.

举个例子。如果要导出定义,但不希望在完成某些异步代码之前使用类定义,则可以执行以下操作:

As an example. If you want to export a class definition, but don't want the class definition used until some async code has completed, you can do something like this:

// do async initialization and keep promise
let e;
const p = APromiseFromAnotherModule().then(val => e = val);

class E {
    constructor() {
        if (e) {
            //...
        } else {
            //...
        }
    }
};

// export constructor function
export default function() {
   return p.then(e => {
       // after async initialization is done, resolve with class E
       return E;
   });
}

调用者可以这样使用它:

The caller could then use it like this:

import init from 'myModule';
init().then(E => {
   // put code here that uses E
}).catch(err => {
   console.log(err);
   // handle error here
});

这解决了几个问题:


  1. 异步初始化是在模块加载后立即启动的。

  2. 在完成异步初始化之前,调用程序无法使用类E,所以在它完成之前不能使用它准备好了

  3. 模块的所有用户使用相同的类E

  4. 异步初始化是缓存的,所以它只进行一次

  1. Asynchronous initialization is started immediately upon module loading.
  2. class E is not made available to the caller until the async initialization is done so it can't be used until its ready
  3. The same class E is used for all users of the module
  4. The async initialization is cached so it's only done once

这篇关于承诺完成后出口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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