我想知道是否可以像下面那样做,或者我应该怎么做。对于那些问为什么的人,这是因为我可以在我的服务中保证返回的数据将根据我的文件需要进行处理和调整。
你能帮我吗?XD
场景 01 == 我所拥有的 ==
我的文件
methodReturnPromise()
.then( data => { /* Sucess, Do This */} )
.catch( err => { /* Failure, Do This Instead */} );
场景02==我想要的==
服务.ts
myServiceMethod(): Promise<any> {
methodReturnPromise()
.then( data => { /* Sucess, Treat and return */} )
.catch( err => { /* Failure, Do This Instead and return */} );
}
我的文件
this.myService.myServiceMethod()
.then( data => { /* Success, Do this */})
.catch( err => { /* Failure, Do This Instead */} );
您可以多次使用then
和catch
。所以改变这样myServiceMethod()
的事情:
myServiceMethod(): Promise<any> {
return methodReturnPromise()
.then( data => {
/* Success, do something here and return the same data received */
return data;
} )
.catch( err => {
/* Failure, do something here and rethrow the same error */
throw err;
} );
}
var promise1 = new Promise(function(resolve, reject) {
resolve("ok");
});
promise1.then(function(data) {
console.log("log1", data);
return data;
})
.then(function(data) {
console.log("log2", data);
});
var promise2 = new Promise(function(resolve, reject) {
reject("ERROR");
});
promise2.catch(function(err) {
console.error("error1", err);
throw err;
})
.catch(function(err) {
console.error("error2", err);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句