Promise returns only the resolved values

Viewed 74

Although I know promises in JS, return a single object. This is just me playing around with it on my chrome inspector and wondering why it will only return the resolved value(Explanation needed)

const soccerPlayer = ["Messi", "Maradona", "Lebron", "Mane"];

const myPromise = new Promise(( resolve, reject) => {
    soccerPlayer.forEach( element => {
        if( element.startsWith("M")) {
            resolve("Thats a soccer star");
        } else {
            reject("Not a soccer star");
        }
    })
});

myPromise                                                                                                   
   .then(res => console.log(res));                                                                            
   .catch(err => console.log(err));

This will only print "Thats a soccer star" although "Lebron" doesnt start with m and it should be rejected

1 Answers

A promise has an internal state. It basically transitions from "pending" to "fulfilled" or "rejected". Once it is in one of the latter two states it stays there. A "fulfilled" promise cannot transition to "fulfilled" again. So subsequent calls to resolve or reject are simply ignored.

Related