拒绝 JavaScript 承诺

时间:2021-05-12 06:34:07

标签: javascript angular promise

我正在构建一个 Angular 应用程序,其中有一个用于从画布中选择项目的服务。一旦用户单击允许他开始选择的按钮,我还会显示一个快捷栏,通知用户他处于选择模式,他可以开始选择。小吃店有一个取消按钮,可以停止选择模式。问题是选择服务返回一个 Promise,如果用户从小吃栏中取消选择模式,我必须拒绝该承诺。代码:

public startSelecting() {
  this.showSnackbar();
  
  // I wait for the user to select something
  this.selectionService.getSelection().then(selection => {
    // If the user selects an item I can close the snackbar
    this.snackbarService.close();

    // do something with the selection
  });
}

private showSnackbar() {
  this.snackbarService.show().subscribe(close => {
    // This is an observable that emits if the users cancels the snackbar,
    // therefore the selecting mode. Here I want to cancel the Promise from above
  });
}

在此先感谢您的帮助!

0 个答案:

没有答案