异步函数中的我的await语句是对jQuery的$.post()方法的调用,该方法返回一个有效的promise,但是我在TypeScript中遇到了这个错误:
Type of ‘await’ operand must either be a valid promise or must not contain a callable ‘then’ member.
我的功能是这个(示例简化).代码有效且有效,但我在TS控制台中收到错误.
async function doAsyncPost() {
const postUrl = 'some/url/';
const postData = {name: 'foo',value: 'bar'};
let postResult;
let upateResult;
function Failed(message: string,body?: string) {
console.log('error: ',message,' body: ',body);
}
function promiseFunc() {
return new Promise( resolve => {
// ... do something else....
resolve();
});
};
function finish() {
// ... do something at the end...
}
try {
// The error is on the $.post()
postResult = await $.post(postUrl,$.param(postData));
if (postResult.success !== 'true') {
return Failed('Error as occoured','Description.....');
}
await promiseFunc();
return finish();
} catch (e) {
await Failed('Error as occoured','Description.....');
}
}
我猜TS有$.post()的问题,因为你可以调用.then(),但是如何解决这个问题呢?此外,在更新2.4.2之前,我没有此错误.