我刚刚开始学习角度知识,我发现我的公司项目具有以下代码:
currentCDFlow: CDCurrentFlowDTO;
this.currentCDFlow = await this.serivce.getCDMaster();
和getCDMaster()代码为
public async getCDMaster(): Promise<CDCurrentFlowDTO> {
return await this.http.post<CDCurrentFlowDTO>(Constants.BASE_HREF + '/getCDMaster', null, this.RequestJson).toPromise().catch(err => { throw err });
}
当getCDMaster()返回Promise<CDCurrentFlowDTO>
(一个Promise对象)时,为什么它可以分配给CDCurrentFlowDTO对象?我希望它只能分配给Promise<CDCurrentFlowDTO>
的对象,而不能仅分配给CDCurrentFlowDTO
(没有被Promise <...>括起来)。
与Java中一样,
String a = new ArrayList<String>();
应该有编译错误...
这是打字稿功能吗?
答案 0 :(得分:1)
原因是Promise
。一个promise是一个表示异步返回的对象,或者可以说是某个事物的一个promise。您的代码在分配中起作用的原因是由于函数调用后面的await
语法。与Promise<T>
一起使用的await语法使执行过程等待承诺完成,或者最终返回它们“承诺”的对象,这就是它起作用的原因...
查看本文以更好地了解... https://www.positronx.io/angular-8-es-6-typescript-promises-examples/
好的,以您的代码作为工作示例...您已定义以下功能
public async getCDMaster(): Promise<CDCurrentFlowDTO> {
return await this.http.post<CDCurrentFlowDTO>(Constants.BASE_HREF + '/getCDMaster', null, this.RequestJson).toPromise().catch(err => { throw err });
}
/*
and then you call it here...
*/
// First you declare the variable to be of type CDCurrentFlowDTO but your
// function will return a Promise<CDCurrentFlowDTO> so let us see how this
// works out...
currentCDFlow: CDCurrentFlowDTO;
/*
Since the await syntax will cause the function call to wait for the returning Promise to complete then having the call like below will work
*/
this.currentCDFlow = await this.serivce.getCDMaster();
/*
But if we were to call it without the await:
*/
this.service.getCDMaster() // we then expect the Promise<CDCurrentFlowDTO> to return
如果我们仍然想异步执行,我们可以例如...
//...
currentCDFlow: CDCurrentFlowDTO;
this.service.getCDMaster().then((result) => {
currentCDFlow = result;
//if we log here we should see the value
console.log(currentCDFlow);
});
// but if we log here we expect undefined because the Promise has not returned yet
console.log(currentCDFlow);
您不必为函数定义async
,因为定义为async
的任何函数都将返回Promise
,但是返回值已经是{{1} },但您仍在等待...您可以改为定义它
Promise
答案 1 :(得分:1)
async / await语法允许从承诺中设置currentCDFlow
。这是ES6中的功能。异步函数的前缀为async
关键字,await
会中止执行,直到实现诺言并解开返回的值。
答案 2 :(得分:0)
回答您的原始问题
等待
使用await
将从承诺中退回返回的值。
所以这行代码
this.currentCDFlow = await this.serivce.getCDMaster();
实际上将方法中解析的Promise值分配给this.currentCDFlow
。
如果您在分配中未使用await
,则TS会投诉,因为您正试图将承诺分配给非承诺对象。
currentCDFlow: CDCurrentFlowDTO;
this.currentCDFlow = this.serivce.getCDMaster(); //TS error
回答您的第二个问题
异步
当您使用async
关键字声明一个函数时,无论您在内部做什么(因此包括Promise
),它将始终返回return await ...
。
看看here:
异步函数总是返回一个承诺。如果异步函数的返回值不是显式的Promise,则将其隐式包装在Promise中。
取决于所使用的JS版本,它可以由JS(从ES8开始)本机处理,也可以通过已编译的TS代码处理。
例如,下面的所有函数将返回Promise。您可以在浏览器的控制台中尝试使用它们
async function test()
{
return "value";
}
async function test2()
{
return Promise.resolve("value");
}
async function test3()
{
return await Promise.resolve("value");
}
console.log(test()); //Promise {<resolved>: "value"}
console.log(test2()); //Promise {<resolved>: "value"}
console.log(test3()); //Promise {<resolved>: "value"}
因此return myPromise
和return await myPromise
通常是相同的,除了here所解释的try/catch
块之类的例外。