本文整理汇总了TypeScript中react-jhipster.isPromise函数的典型用法代码示例。如果您正苦于以下问题:TypeScript isPromise函数的具体用法?TypeScript isPromise怎么用?TypeScript isPromise使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isPromise函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: default
export default () => next => action => {
// If not a promise, continue on
if (!isPromise(action.payload)) {
return next(action);
}
/**
*
* The error middleware serves to dispatch the initial pending promise to
* the promise middleware, but adds a `catch`.
* It need not run in production
*/
if (process.env.NODE_ENV === 'development') {
// Dispatch initial pending promise, but catch any errors
return next(action).catch(error => {
console.error(`${action.type} caught at middleware with reason: ${JSON.stringify(error.message)}.`);
if (error && error.response && error.response.data) {
const message = getErrorMessage(error.response.data);
console.error(`Actual cause: ${message}`);
}
return Promise.reject(error);
});
}
return next(action);
};
示例2: default
export default () => next => action => {
// If not a promise, continue on
if (!isPromise(action.payload)) {
return next(action);
}
/**
*
* The notification middleware serves to dispatch the initial pending promise to
* the promise middleware, but adds a `then` and `catch.
*/
return next(action).then(response => {
if (action.meta && action.meta.successMessage) {
// toast(action.meta.successMessage, { type: toast.TYPE.SUCCESS });
}
return Promise.resolve(response);
}).catch(error => {
if (action.meta && action.meta.errorMessage) {
// toast(action.meta.errorMessage, { type: toast.TYPE.ERROR });
}
return Promise.reject(error);
});
};
示例3: default
export default () => next => action => {
// If not a promise, continue on
if (!isPromise(action.payload)) {
return next(action);
}
/**
*
* The notification middleware serves to dispatch the initial pending promise to
* the promise middleware, but adds a `then` and `catch.
*/
return next(action)
.then(response => {
if (action.meta && action.meta.successMessage) {
toast.success(action.meta.successMessage);
} else if (response && response.action && response.action.payload && response.action.payload.headers) {
const headers = response.action.payload.headers;
let alert: string = null;
let alertParams: string = null;
Object.entries(headers).forEach(([k, v]: [string, string]) => {
if (k.endsWith('app-alert')) {
alert = v;
} else if (k.endsWith('app-params')) {
alertParams = v;
}
});
if (alert) {
const alertParam = alertParams;
toast.success(translate(alert, { param: alertParam }));
}
}
return Promise.resolve(response);
})
.catch(error => {
if (action.meta && action.meta.errorMessage) {
toast.error(action.meta.errorMessage);
} else if (error && error.response) {
const response = error.response;
const data = response.data;
if (!(response.status === 401 && (error.message === '' || (data && data.path && data.path.includes('/api/account'))))) {
let i;
switch (response.status) {
// connection refused, server not reachable
case 0:
addErrorAlert('Server not reachable', 'error.server.not.reachable');
break;
case 400:
const headers = Object.entries(response.headers);
let errorHeader = null;
let entityKey = null;
headers.forEach(([k, v]: [string, string]) => {
if (k.endsWith('app-error')) {
errorHeader = v;
} else if (k.endsWith('app-params')) {
entityKey = v;
}
});
if (errorHeader) {
const entityName = translate('global.menu.entities.' + entityKey);
addErrorAlert(errorHeader, errorHeader, { entityName });
} else if (data !== '' && data.fieldErrors) {
const fieldErrors = data.fieldErrors;
for (i = 0; i < fieldErrors.length; i++) {
const fieldError = fieldErrors[i];
if (['Min', 'Max', 'DecimalMin', 'DecimalMax'].includes(fieldError.message)) {
fieldError.message = 'Size';
}
// convert 'something[14].other[4].id' to 'something[].other[].id' so translations can be written to it
const convertedField = fieldError.field.replace(/\[\d*\]/g, '[]');
const fieldName = translate(`demoGatewayApp.${fieldError.objectName}.${convertedField}`);
addErrorAlert(`Error on field "${fieldName}"`, `error.${fieldError.message}`, { fieldName });
}
} else if (data !== '' && data.message) {
addErrorAlert(data.message, data.message, data.params);
} else {
addErrorAlert(data);
}
break;
case 404:
addErrorAlert('Not found', 'error.url.not.found');
break;
default:
if (data !== '' && data.message) {
addErrorAlert(data.message);
} else {
addErrorAlert(data);
}
}
}
} else if (error && error.message) {
toast.error(error.message);
} else {
toast.error('Unknown error!');
}
return Promise.reject(error);
});
};