typescript的ajax同步,如何在async / await和typescript中使用jQuery的$.post()方法
异步函数中的我的await语句是对jQuery的$.post()方法的调用,该方法返回一个有效的promise,但是我在TypeScript中遇到了这个错误:Type of ‘await’ operand must either be a valid promise or must not contain a callable ‘then’ member.我的功能是这个(示例简化).代码有效且有效
异步函数中的我的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之前,我没有此错误.
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)