typescript定义类型和async之间怎么配合使用
本文介绍了typescript定义类型和async之间怎么配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
interface Obj{
title: string,
location: string
}
function getPlace(){
return new Promise(function(resolve, reject){
resolve({
title: "titleABC",
location: "locationABC"
})
})
}
async function showPlace(){
const place:Obj = await getPlace();
return place;
}
这里会报错
解决方案
async function test1(): Promise<string> {
return "hello";
}
async function test2(): Promise<any> {
return new Promise(resolve => {
setTimeout(() => {
resolve("world");
}, 200);
});
}
(async () => {
const h: string = await test1();
const w: any = await test2();
console.log(h, w);
})();
这篇关于typescript定义类型和async之间怎么配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文