Next.js Discord

Discord Forum

why does the subsequent code still need to perform a switch on the promise status?

Unanswered
Longtail tuna posted this in #help-forum
Open in Discord
Avatar
Longtail tunaOP
In React's source code: the asynchronicity of the then function, since the status has already been set to pending earlier, why does the subsequent code still need to perform a switch on the status?

            thenableState = thenable;
            thenableState.status = "pending";
            thenableState.then(
              function (fulfilledValue) {
                if ("pending" === thenable.status) {
                  var fulfilledThenable = thenable;
                  fulfilledThenable.status = "fulfilled";
                  fulfilledThenable.value = fulfilledValue;
                }
              },
              function (error) {
                if ("pending" === thenable.status) {
                  var rejectedThenable = thenable;
                  rejectedThenable.status = "rejected";
                  rejectedThenable.reason = error;
                }
              }
            );
          }
          switch (thenable.status) {
            case "fulfilled":
              return thenable.value;
            case "rejected":
              throw (
                ((thenableState = thenable.reason),
                checkIfUseWrappedInAsyncCatch(thenableState),
                thenableState)
              );
          }

0 Replies