在Typescript / JavaScript中展开Promise对象
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在Typescript / JavaScript中展开Promise对象,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2899字,纯文字阅读大概需要5分钟。
内容图文
![在Typescript / JavaScript中展开Promise对象](/upload/InfoBanner/zyjiaocheng/797/5cc569451b0b44f0afd2e60858298770.jpg)
我是JavaScript / TypeScript Promise返回类型的新手.
问题是我正在托管一个rest API端点,它在内部调用另一个使用JS Request模块返回Promise对象的服务的API.
我想打开内部API promise响应并创建一个新的普通Typescript对象,最后返回新修改的响应对象而不是Promise.
以下是描述我的问题的示例代码:
export class SampleClass {
public process(): NewResponse {
const newResponse: NewResponse = new NewResponse();
// invokeOtherAPI() call Returns a Promise by invoking a different endpoint
invokeOtherAPI().then(function (result) {
newResponse.propertyOne = result.propertyOne;
newResponse.propertyTwo = result.propertyTwo;
});
return newResponse;
}}
在这种情况下,process()返回一个空值,虽然它在then()内部分配.
这是我的REST API控制器类(缺少路径装饰器)
export class SampleController() {
const service: SampleClass = new SampleClass();
public get(): NewResponse {
return service.process();
}
}
问题:
>从Typescript API一直返回Promise对象是一个好习惯吗?
>有没有办法在内部解包promise对象并从API返回非promise对象?
感谢您的帮助.
解决方法:
I want to unwrap the internal API promise response and create a new normal Typescript object and finally return the new modified response object rather than a Promise.
你不能.如果你的API函数的任何部分是异步的,那么整个API必须是异步的,这意味着它必须返回一个调用者使用的承诺.then()或await on来获取值,或者你使用旧样式回调和调用者传递一个回调,当异步值可用时,你将调用该回调.
这里有更多的解释:How do I return the response from an asynchronous call?.
- Is it a good practice to return the Promise object all the time from a Typescript API?
是.如果操作是异步的,则返回promise是与异步结果进行通信的理想方式.
- Is there a way I can unwrap a promise object internally and return a non promise object from an API?
不.如果你真的不想使用promises,你可以使用旧的回调样式来回传结果,但是调用者在处理异步结果时会遇到同样的问题,你不能直接从结果返回结果你的API.
在这段代码中:
export class SampleClass {
public process(): NewResponse {
const newResponse: NewResponse = new NewResponse();
// invokeOtherAPI() call Returns a Promise by invoking a different endpoint
invokeOtherAPI().then(function (result) {
newResponse.propertyOne = result.propertyOne;
newResponse.propertyTwo = result.propertyTwo;
});
return newResponse;
}}
事情出错了,因为你返回newResponse的行.此时,newResponse还没有值.您知道它具有值的唯一位置在.then()处理程序内.这就是您无法直接从API返回结果的原因.您的API将在检索到值之前返回.这正是承诺的目的.您返回promise并且调用者在返回的promise上使用.then()来获取值.
我自己不知道TypeScript的方法,所以你可以填写返回值的正确语法,但这是在这里返回的承诺:
export class SampleClass {
public process(): <fill in promise return value type here> {
const newResponse: NewResponse = new NewResponse();
// return promise from this function
return invokeOtherAPI().then(function(result) {
newResponse.propertyOne = result.propertyOne;
newResponse.propertyTwo = result.propertyTwo;
// make newResponse be the resolved value of the promise
return newResponse;
});
}};
内容总结
以上是互联网集市为您收集整理的在Typescript / JavaScript中展开Promise对象全部内容,希望文章能够帮你解决在Typescript / JavaScript中展开Promise对象所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。