如何将诺言作为参数传递

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

我想将promise作为参数传递给另一个函数,将其用作回调。我拥有的代码如下:

Component 1
function1() {
    let promise: Promise<any>;
    this.function2(promise);
    promise.then(response => console.log(response));
}

Component 2
function2(promise) {
    // Some Code 
    this.dialog.afterClosed().subscribe(data => {
       promise.resolve(data);
    });
}

以这种方式执行此操作会在函数1中产生错误,错误是:无法读取未定义的属性'then'

angular promise rxjs angular-promise
1个回答
0
投票

也许您想做的是像这样:

function1() {
  const promise: Promise = new Promise((resolve, reject) => {
    this.function2(resolve);
  });

  promise.then(response => console.log(response));
}

function2(resolveFn: any) {
    // Some Code 
    this.dialog.afterClosed().subscribe(data => {
       resolveFn(data);
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.