У меня есть реализация обещанной гонки за тайм-аут.Я хочу записать ошибку тайм-аута в случае тайм-аута.
Проблема заключается в том, что он регистрирует журнал даже тогда, когда выборка прошла успешно, поскольку он работает параллельно и все еще выполняется после тайм-аута.
Где разместить errorLogger.info(message)
, чтобы он не выполнялся в случае non-timeout
?Я думаю, что я неправильно это формулирую, поэтому он выводит данные до того, как он на самом деле отвергает.
return Promise.race([
fetch(url, options)
.then(function (response) {
if (response.status >= 400) {
const message = `fetch-utility.js: bad server response while fetching url=${url} with options=${options}.`;
errorLogger.error(`${message} Response: status=${response.status} statusText:${response.statusText}.`);
throw new BadServerResponseException(message, response.status);
}
return response;
}),
new Promise((_, reject) =>
setTimeout(() => {
const message = `fetch-utility.js: timeout happened while fetching details url=${url} with options=${options}.
The timeout set is ${timeout}.`;
// TODO: this gets logged even the parallel wins - need to see better way to log this
// errorLogger.error(message);
reject(new TimeoutException(message));
}, timeout),
),
]);