【问题标题】:Using Async/Await to Control Order of Operations使用 Async/Await 控制操作顺序
【发布时间】:2019-01-16 05:19:41
【问题描述】:

我试图想出一种方法来确保在触发另一个函数之前完成某个操作。在我的代码中,我触发了一个 API 请求来更新数据,然后我根据第一个请求的结果计算一些其他值。

但是,我有时会遇到第一个请求不会在第二个函数触发之前完成,因此数据最终会损坏并且不同步。我一直在用第二个函数超时来临时处理这个问题。但是,我想使用 async/await 代替,因为它显然是处理此类事情的更好(且有保证)的方式——因为假设超时会起作用,但它可能不会起作用,这绝不是好事。

这是我目前拥有的:

private async moveRecord(stage) {
    if (!this.customer || !this.selectedService) return;
    let targetService = this.selectedService.service;
    let staffStarted = await this.userAccessService.getTargetId();

    this.clientMoveService.moveClientRecord(this.client._id, category, targetService, staffStarted);

    setTimeout(() =>
    {
        this.getNextStageOperation();
    }, 1000);
}

如何调整此代码以使用 async/await 仅在第一个请求 this.clientMoveService.moveClientRecord() 完成后触发 this.getNextOperation

我可以简单地这样做吗?

private async moveRecord(stage) {
    if (!this.customer || !this.selectedService) return;
    let targetService = this.selectedService.service;
    let staffStarted = await this.userAccessService.getTargetId();

    await this.clientMoveService.moveClientRecord(this.client._id, category, targetService, staffStarted);

    this.getNextStageOperation();
}

为了更清楚起见,moveClientRecord() 是通过套接字发出的请求,如下所示:

public moveClientRecord(clientId: string, category: string, service: string, staffStarted: string)
{
    let args = { id: clientId, category: category, service: service, staff: staffStarted };
    console.log('args: ', args);

    API.service.cast({
        eventName: `clients.updateStatus`,
        args: args,
        listener: function (data)
        {
            // Ignore if socket disconnects
            if (data === 'disconnect' || data === 'error') return;

            // Return true to stop event listener
            return true;

        }.bind(this)
    });
}

【问题讨论】:

  • moveClientRecord 返回什么?
  • 它更新后端的数据,然后将其用作标准的一部分以返回getNextStageOperation请求的数据。换句话说,第一个操作必须在第二个操作开始之前完成。
  • 你应该等待 clientMoveService
  • 如何“确保”moveClientRecord() 正在返回一个承诺?
  • @Muirik 好吧,您还没有发布moveClientRecord 代码,所以我们无法具体说明,但大概您可以访问该代码,是吗?所以看看它返回了什么。如果它返回返回承诺的异步调用的结果,那很好。如果没有,您应该将返回值包装在Promise.resolvePromise.reject 中(取决于该值是代表成功还是失败)。

标签: javascript asynchronous async-await


【解决方案1】:

它会这样工作:

private async moveRecord(stage) {
    if (!this.customer || !this.selectedService) return;
    let targetService = this.selectedService.service;
    let staffStarted = await this.userAccessService.getTargetId();

    // must return a Promise
    await this.clientMoveService.moveClientRecord(this.client._id, 
    category, targetService, staffStarted);

    this.getNextStageOperation();
}

【讨论】:

    【解决方案2】:

    如果 this.clientMoveService.moveClientRecord 返回一个承诺,你可以这样做

    await this.clientMoveService.moveClientRecord(this.client._id, category, targetService, staffStarted);
    

    【讨论】:

      猜你喜欢
      • 2022-08-14
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 2019-01-03
      • 2017-11-03
      相关资源
      最近更新 更多