编写更清洁的 Promise 完成闭包
Writing cleaner Promise completion closure
Angular 和承诺的新手,但我意识到我已经做了很多:
this.myService.createFoo(data).subscribe(
(result: any) => {
resolve({success: true, data: result});
},
err => {
resolve({success: false, message: err});
});
this.myService.updateFoo(data).subscribe(
(result: any) => {
resolve({success: true, data: result});
},
err => {
resolve({success: false, message: err});
});
我该如何重写我的订阅方法的完成闭包,这样我就不会重复这样的代码?。谢谢!
一种方法是将您的回调提取到通用函数中并传递它们:
const successHandler = (data: any) => resolve({success: true, data });
const errorHandler = (message) => resolve({success: false, message });
this.myService.createFoo(data).subscribe(successHandler, errorHandler);
this.myService.updateFoo(data).subscribe(successHandler, errorHandler);
如果您想在 Promise
的范围之外定义这些(我假设您发布的代码在其中),您可以柯里化处理程序:
some-util-library.js
const handlers = resolve => ({
success: (data: any) => resolve({success: true, data }),
error: (message) => resolve({success: false, message })
});
export { handlers };
otherfile.js
import { handlers } from './some-util-library.js';
... other code
const { success, error } = handlers(resolve);
this.myService.createFoo(data).subscribe(success, error);
this.myService.updateFoo(data).subscribe(success, error);
我假设您正在尝试 return 一个承诺,并且您发布的代码包含在一个看起来像这样的方法中:
create(): Promise<any> {
const data = 'whatever';
return new Promise<any>((resolve, reject) => {
this.myService.createFoo(data).subscribe(
(result: any) => {
resolve({ success: true, data: result });
},
err => {
resolve({ success: false, message: err });
});
});
}
如果是这种情况,您可以使用 toPromise
运算符将 Observable 变成一个 Promise,而不是手动创建一个 Promise。如果您还没有导入它,则需要先导入它:
import 'rxjs/add/operator/toPromise';
然后,将它与async
和await
结合起来,你可以这样改造你的代码:
async create(): Promise<any> {
const data = 'whatever';
try {
const result = await this.myService.createFoo(data).toPromise();
return { success: true, data: result }
} catch (err) {
return { success: false, message: err };
}
}
的完美用例
Angular 和承诺的新手,但我意识到我已经做了很多:
this.myService.createFoo(data).subscribe(
(result: any) => {
resolve({success: true, data: result});
},
err => {
resolve({success: false, message: err});
});
this.myService.updateFoo(data).subscribe(
(result: any) => {
resolve({success: true, data: result});
},
err => {
resolve({success: false, message: err});
});
我该如何重写我的订阅方法的完成闭包,这样我就不会重复这样的代码?。谢谢!
一种方法是将您的回调提取到通用函数中并传递它们:
const successHandler = (data: any) => resolve({success: true, data });
const errorHandler = (message) => resolve({success: false, message });
this.myService.createFoo(data).subscribe(successHandler, errorHandler);
this.myService.updateFoo(data).subscribe(successHandler, errorHandler);
如果您想在 Promise
的范围之外定义这些(我假设您发布的代码在其中),您可以柯里化处理程序:
some-util-library.js
const handlers = resolve => ({
success: (data: any) => resolve({success: true, data }),
error: (message) => resolve({success: false, message })
});
export { handlers };
otherfile.js
import { handlers } from './some-util-library.js';
... other code
const { success, error } = handlers(resolve);
this.myService.createFoo(data).subscribe(success, error);
this.myService.updateFoo(data).subscribe(success, error);
我假设您正在尝试 return 一个承诺,并且您发布的代码包含在一个看起来像这样的方法中:
create(): Promise<any> {
const data = 'whatever';
return new Promise<any>((resolve, reject) => {
this.myService.createFoo(data).subscribe(
(result: any) => {
resolve({ success: true, data: result });
},
err => {
resolve({ success: false, message: err });
});
});
}
如果是这种情况,您可以使用 toPromise
运算符将 Observable 变成一个 Promise,而不是手动创建一个 Promise。如果您还没有导入它,则需要先导入它:
import 'rxjs/add/operator/toPromise';
然后,将它与async
和await
结合起来,你可以这样改造你的代码:
async create(): Promise<any> {
const data = 'whatever';
try {
const result = await this.myService.createFoo(data).toPromise();
return { success: true, data: result }
} catch (err) {
return { success: false, message: err };
}
}