我想将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'
也许您想做的是像这样:
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);
});
}