如何强制解析异步功能

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

我正在使用Sails.js来构建我的API,因此,作为风帆的一部分,要检索模型,我可以覆盖或修改customToJSON方法。这是问题,customToJSON方法是一种同步方法,但我在里面执行一些异步操作。有我的代码:

async customToJSON() {
  // set the interpolate characteres to `{{` and `}}`
  _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
  const NOTIFICATION_TYPE = notificationTypes[this.type];

  // --->>> THIS IS THE ASYNC METHOD <<<---
  const templateParams = await Notification._getTemplateParams(this);

  // compile templates
  const compiledLink = _.template(
    NOTIFICATION_TYPE.link, { imports: templateParams }
  );
  const compiledTitle = _.template(
    NOTIFICATION_TYPE.title, { imports: templateParams }
  );
  const compiledDescription = _.template(
    NOTIFICATION_TYPE.description, { imports: templateParams }
  );

  return {
    ..._.omit(this, ['relatedModels']),
    _params: templateParams,
    link: compiledLink(templateParams),
    title: compiledTitle(templateParams),
    description: compiledDescription({}),
  }
},

它不起作用,因为sails旨在将此方法作为同步运行。所以问题是:有没有办法强制解决承诺?

我正在使用qazxsw poi和qazxsw poi和qazxsw poi

javascript node.js asynchronous sails.js
1个回答
0
投票

看着node v8.10

目前,customToJSON函数不支持异步功能。

(点击链接了解更多详情)

所以你不能在你的sails v1中拥有异步代码。相反,你应该先调用你的action2函数。

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