다음 서비스가 TypeScript에서 백엔드에서 데이터를 가져옵니다.올바른 약속 처리 성공 및 오류 콜백
getAllPropertiesByAppId 함수의 매개 변수로서 성공 및 오류 콜백이 있습니다.
export class PropertyService implements IPropertyService {
/**
* Get all properties
*/
public getAllPropertiesByAppId(appliactionId: string, success: (properties: Array<IPropertyItem>) => void, error: (error: any) => void): void {
//set up createRequestStr and requestInit
fetch(createRequestStr, requestInit)
.then<IPropertyItem[]>((response: Response) => {
if (response.status===401) {
throw new UnAuthorizedException();
}
return response.json<IPropertyItem[]>();
})
.then((response: IPropertyItem[]) => {
success(response);
})
.catch((reason: any) => {
//error handling
}
});
}
는 그런 내 행동 작성자에이 서비스를 사용하고 있습니다 :
initProperties: (appId: string): ActionCreator => (dispatch: Redux.Dispatch, getState:() => IApplicationState) => {
"use strict";
console.log("ShoppingCart initProperties - Request all Property");
var service: IPropertyService = kernel.get<IPropertyService>("IPropertyService");
service.getAllPropertiesByAppId(appId, (properties: Array<IPropertyItem>): void => {
dispatch(new ShoppingCartPropertiesLoaded(appId, properties));
dispatch(new ShoppingCartPropertiesDone(appId, System.Init.Done));
}, (error: any): void => {
console.log("ShoppingCart initProperties - error:" + error);
dispatch(new ShoppingCartPropertiesDone(appId, System.Init.Error));
});
}
그래서 나는 getAllPropertiesByAppId를 호출하고 모든 것이 괜찮 때 조치를 ShoppingCartPropertiesLoaded 및 ShoppingCartPropertiesDone 파견 initProperties 액션 창조자를 호출 할 때.
내가 저장하기 위해 연결 간단한 구성 요소를 가지고 렌더링 방법은
export default class TotalPriceList extends React.Component<ITotalPriceListProps, void> {
public render(): JSX.Element {
throw 'SomeError';
}
}
처리되지 않은 예외가 가져 오기의 catch 문에 끝을 실행하는 경우 구성 요소 오류가 발생합니다.
약속을 종료하는 방법과 같은 것을 놓쳤는가? 가져 오기의 catch 문에서 콜백 예외를 피하기 위해 약속 이외의 함수/콜백을 호출하는 방법이 빠졌습니까? 당신이 약속 체인 내부 예외를 캐치하지 않으려면 당신의 도움을 주셔서 대단히
덕분에
완벽한을 사용하십시오! 감사 Bergi! –