Czemu ta obietnica ma status rejected, a nie resolve?

0

Czemu ta obietnica ma status rejected, a nie resolve?

const promise = Promise.resolve(Promise.reject(Error('exit')));
console.log(promise)

Wewnętrzna obietnica zwraca rejected. Jednak obietnica zewnętrzna przecież się wykona i zwróci rezultat. Czemu ona zwraca rejected, a nie resolve?

2

Ponieważ resolve nie jest przeciwstawne do reject. W związku ze promisa reject zwrocila wartosc, to promisa resolve ci ja przekazala. resolve pokazuje wartosc z callback prawda ? Musialbys zrobic Promise.reject(Error('exit')).than.resolve...

1

Wystarczy zajrzeć do dokumentacji https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve.

The Promise.resolve() method returns a Promise object that is resolved with a given value. If the value is a promise, that promise is returned; if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value. This function flattens nested layers of promise-like objects (e.g. a promise that resolves to a promise that resolves to something) into a single layer.

czyli mówiąc w skrócie - tak to ma działać

1 użytkowników online, w tym zalogowanych: 0, gości: 1