来自Promise.All的JSDoc数组

问题描述 投票:0回答:1

我尝试记录Promise.all结果,因为一旦我解构数据,类型就会丢失。

这是一个例子,(我试图欺骗它将promise放在一个闭包中)

      const promiseAll = () => {
        return Promise.all([
          this.b2bCompanies.getCompany(companyId, token),
          this.b2BFacade.getProfile(profileId, token),
        ]);
      };

      const [company, profile] = await promiseAll();

原始代码是:

      const [company, profile] = await Promise.all([
        this.b2bCompanies.getCompany(companyId, token),
        this.b2BFacade.getProfile(profileId, token),
      ]);

该公司和配置文件成为*this.b2bCompanies.getCompany返回CompanySchema和配置文件称为ProfileSchema

任何想法如何实现这一点并保持代码输入正确?

谢谢 :)

node.js types promise jsdoc
1个回答
0
投票

ES6 externs for Closure Compiler以这种方式定义Promise.all;

/**
 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
 * @param {!Iterable<VALUE>} iterable
 * @return {!Promise<!Array<RESULT>>}
 * @template VALUE
 * @template RESULT := mapunion(VALUE, (V) =>
 *     cond(isUnknown(V),
 *         unknown(),
 *         cond(isTemplatized(V) && sub(rawTypeOf(V), 'IThenable'),
 *             templateTypeOf(V, 0),
 *             cond(sub(V, 'Thenable'), unknown(), V))))
 * =:
 */
Promise.all = function(iterable) {};

虽然我认为用这种方式定义它会起作用,但我想知道你是否可以包含这个外部因素?当然,只有在使用Closure Compiler时才有意义。

© www.soinside.com 2019 - 2024. All rights reserved.