本文整理匯總了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);
});
};