无法在承诺中调用异步函数
Cannot call async function within a promise
我正在尝试在 promise 中调用异步函数,但是 .then() 中的代码从未到达,.catch() 也没有,也没有任何调试信息,所以我在损失.
如果我在代码的其他地方调用 同一个异步函数,它会工作!
我怀疑我错误地实现了 bluebird 承诺,但我在我的代码中看不到任何反模式。
这是一些伪代码,基本上就是我的代码在做什么:
// application entrypoint
public static ApplicationEntryPoint() {
this.MainFunction()
.then((result: any) => {
console.log(SuccessStrings[SuccessStrings.cycle_completed].toString());
this.ApplicationEntryPoint();
}).catch((error: any) => {
this.ApplicationEntryPoint();
});
}
public static MainFunction() {
return new Promise((resolve: any, reject: any) => {
// other code is here....
this.execute()
.then(result => {
return this.execute2(result);
}).then((result2: Array<any>) => {
return this.problemfunction(result2);
}).then((result) => {
resolve(result);
}).catch((error) => {
reject(error);
});
});
}
public static ProblemFunction(result2) {
return new Promise((resolve: any, reject: any) => {
// other code in here....
someAsyncCall()
.then(() => {
let resultArray = this.PromisifyCalculations(result2);
return resultArray;
}).then((resultArray) => {
// do some code here
resolve(resultArray);
});
});
}
public static PromisifyCalculations(result2) {
// Call CalculationFunction() for each element
let CoinPromises: Array<any> = new Array<any>();
CoinPromises = result2.map(item => () => this.CalculationFunction(item));
return Promise.all(m_arCoinPromises.map(item => item()));
}
public static CalculationFunction() {
**// here is where the Async call DOES NOT work FOR each element..**
return new Promise((resolve: any, reject: any) => {
dataLookup(id, baseid)
.then((resultantArray) => {
// never reached
resolve(resultantArray);
}).catch((error) => {
// never reached either
reject(error);
});
});
}
谢谢!
return new Promise((resolve: any, reject: any) => {
那是反模式!您不需要 Promise 构造函数,除非您将回调包装到其中。在所有其他情况下,您可以 return 链:
return this.execute()
.then(result => {
return this.execute2(result);
}).then((result2: Array<any>) => {
return this.problemfunction(result2);
})
现在的问题似乎是,你从来没有catch
拒绝这个:
someAsyncCall()
当您以反模式方式包装它时,错误不会冒出来。
我正在尝试在 promise 中调用异步函数,但是 .then() 中的代码从未到达,.catch() 也没有,也没有任何调试信息,所以我在损失.
如果我在代码的其他地方调用 同一个异步函数,它会工作!
我怀疑我错误地实现了 bluebird 承诺,但我在我的代码中看不到任何反模式。 这是一些伪代码,基本上就是我的代码在做什么:
// application entrypoint
public static ApplicationEntryPoint() {
this.MainFunction()
.then((result: any) => {
console.log(SuccessStrings[SuccessStrings.cycle_completed].toString());
this.ApplicationEntryPoint();
}).catch((error: any) => {
this.ApplicationEntryPoint();
});
}
public static MainFunction() {
return new Promise((resolve: any, reject: any) => {
// other code is here....
this.execute()
.then(result => {
return this.execute2(result);
}).then((result2: Array<any>) => {
return this.problemfunction(result2);
}).then((result) => {
resolve(result);
}).catch((error) => {
reject(error);
});
});
}
public static ProblemFunction(result2) {
return new Promise((resolve: any, reject: any) => {
// other code in here....
someAsyncCall()
.then(() => {
let resultArray = this.PromisifyCalculations(result2);
return resultArray;
}).then((resultArray) => {
// do some code here
resolve(resultArray);
});
});
}
public static PromisifyCalculations(result2) {
// Call CalculationFunction() for each element
let CoinPromises: Array<any> = new Array<any>();
CoinPromises = result2.map(item => () => this.CalculationFunction(item));
return Promise.all(m_arCoinPromises.map(item => item()));
}
public static CalculationFunction() {
**// here is where the Async call DOES NOT work FOR each element..**
return new Promise((resolve: any, reject: any) => {
dataLookup(id, baseid)
.then((resultantArray) => {
// never reached
resolve(resultantArray);
}).catch((error) => {
// never reached either
reject(error);
});
});
}
谢谢!
return new Promise((resolve: any, reject: any) => {
那是反模式!您不需要 Promise 构造函数,除非您将回调包装到其中。在所有其他情况下,您可以 return 链:
return this.execute()
.then(result => {
return this.execute2(result);
}).then((result2: Array<any>) => {
return this.problemfunction(result2);
})
现在的问题似乎是,你从来没有catch
拒绝这个:
someAsyncCall()
当您以反模式方式包装它时,错误不会冒出来。