Ecco il codice in questione:"Uncaught (nella promessa)" quando si chiama la funzione di scarto all'interno di un metodo prendere 'poi'
new Promise((resolve, reject) => {
const opts = {
credentials: 'same-origin',
};
fetch(`/_api/myAPI`, opts)
.then((res) => {
if (!res.ok) {
reject(res);
} else {
...
Se l'URL genera un'eccezione a 401, quando l'esecuzione raggiunge reject(res);
getta Uncaught (in promise)
Anche dopo aggiungo un .catch
dopo la chiamata .then
, cioè
fetch(`/_api/myAPI`, opts)
.then((res) => {
if (!res.ok) {
reject(res);
} else {
...
})
.catch((e) => {
console.log(e);
}
succede ancora.
Perché reject
genera questa eccezione e come posso risolverlo? La mia esperienza è limitata a jQuery.Promise
e io non un reject
all'interno di un gestore di errori attiverà questo errore.
Wrapping recuperare promessa in una nuova promessa è [un antipattern] (http://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do -i-evitare-it). E causa qui effetti negativi. – estus
@estus Grazie per il commento informativo! –