When Promise state become rejected or resolved












1


















let promise = new Promise(function(resolve, reject) {
setTimeout(() => reject(new Error("Whoops!")), 1000);
});

// reject runs the second function in .then
promise.then(
result => alert(result), // doesn't run
error => alert(error) // shows "Error: Whoops!" after 1 second
);





In the above code snippets even though I am calling reject but promise state is coming as resolved but when I am removing error => alert(error) from promise.then then I am getting promise state as rejected
If one is calling reject then promise state should be rejected not resolved am I correct?










share|improve this question





























    1


















    let promise = new Promise(function(resolve, reject) {
    setTimeout(() => reject(new Error("Whoops!")), 1000);
    });

    // reject runs the second function in .then
    promise.then(
    result => alert(result), // doesn't run
    error => alert(error) // shows "Error: Whoops!" after 1 second
    );





    In the above code snippets even though I am calling reject but promise state is coming as resolved but when I am removing error => alert(error) from promise.then then I am getting promise state as rejected
    If one is calling reject then promise state should be rejected not resolved am I correct?










    share|improve this question



























      1












      1








      1











      let promise = new Promise(function(resolve, reject) {
      setTimeout(() => reject(new Error("Whoops!")), 1000);
      });

      // reject runs the second function in .then
      promise.then(
      result => alert(result), // doesn't run
      error => alert(error) // shows "Error: Whoops!" after 1 second
      );





      In the above code snippets even though I am calling reject but promise state is coming as resolved but when I am removing error => alert(error) from promise.then then I am getting promise state as rejected
      If one is calling reject then promise state should be rejected not resolved am I correct?










      share|improve this question



















      let promise = new Promise(function(resolve, reject) {
      setTimeout(() => reject(new Error("Whoops!")), 1000);
      });

      // reject runs the second function in .then
      promise.then(
      result => alert(result), // doesn't run
      error => alert(error) // shows "Error: Whoops!" after 1 second
      );





      In the above code snippets even though I am calling reject but promise state is coming as resolved but when I am removing error => alert(error) from promise.then then I am getting promise state as rejected
      If one is calling reject then promise state should be rejected not resolved am I correct?






      let promise = new Promise(function(resolve, reject) {
      setTimeout(() => reject(new Error("Whoops!")), 1000);
      });

      // reject runs the second function in .then
      promise.then(
      result => alert(result), // doesn't run
      error => alert(error) // shows "Error: Whoops!" after 1 second
      );





      let promise = new Promise(function(resolve, reject) {
      setTimeout(() => reject(new Error("Whoops!")), 1000);
      });

      // reject runs the second function in .then
      promise.then(
      result => alert(result), // doesn't run
      error => alert(error) // shows "Error: Whoops!" after 1 second
      );






      javascript ecmascript-6 promise es6-promise






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 11:47









      Hassan Imam

      11.7k31330




      11.7k31330










      asked Nov 14 '18 at 11:45









      Alex_AngularAlex_Angular

      445




      445
























          4 Answers
          4






          active

          oldest

          votes


















          1














          The promise that is resolved is not the original promise, but the promise returned by then. The original promise is indeed rejected, and you can verify that by console.log(promise). But because you chained then it returns another promise ..




          Return value [of then]



          A Promise in the pending status. The handler function
          (onFulfilled or onRejected) then gets called asynchronously (as soon
          as the stack is empty). After the invocation of the handler function,
          if the handler function:




          • returns a value, the promise returned by then gets resolved with the
            returned value as its value;


          • doesn't return anything, the promise returned by then gets resolved with an undefined value;

          • throws an error, the promise returned by then gets rejected with the thrown
            error as its value;


          ...




          This second point is what applies to your case. You can verify that by observing that
          Promise.reject().then(undefined, ()=>{})

          returns a promise that gets resolved with an undefined value.






          share|improve this answer

































            2














            You can also handle Promises in "then-catch" fashion like below






            let promise = new Promise(function(resolve, reject) {
            setTimeout(() => reject('Error'));
            });

            promise
            .then(result => console.log('Result ', result))
            .catch(error => console.log('This is Error message -', error))








            share|improve this answer


























            • then accepts two arguments - one for success callback and the other for a rejection callback. Using it a valid way of dealing with Promise rejection.

              – Szab
              Nov 14 '18 at 12:03






            • 1





              Oh!! How did I miss that.. Damn!! Thank you @Szab..

              – Nitish Narang
              Nov 14 '18 at 12:06











            • Edited my answer.. Thank you for the knowledge @Szab .. I never realised this...

              – Nitish Narang
              Nov 14 '18 at 12:08











            • A promise should have exception handling mechanism. That is why we have .catch, the control goes to .catch when an exception is thrown from the promise body. if you call resolve the control goes to the first executor argument of .then and if you call reject, then it goes to the second argument of .then. So I think we should have both then and catch

              – Parnab Sanyal
              Nov 14 '18 at 12:56



















            0














            The final status of the promise is also rejected in your case.
            Try console.log(promise);
            It has nothing to do with error => alert(error)






            share|improve this answer































              0














              The code you posted seems to work correctly both on Chrome and Firefox - the Promise was rejected as expected. reject function marks the Promise as rejected - you can then react upon that rejection either by passing a callback function into a second argument of then or by using the catch method. Both approaches are correct.



              If you are encountering any unexpected behaviour, verify that you are not using some faulty polyfill that replaces the original Promise object.



              Reference: Promise.prototype.then, Promise.prototype.catch






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299500%2fwhen-promise-state-become-rejected-or-resolved%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                The promise that is resolved is not the original promise, but the promise returned by then. The original promise is indeed rejected, and you can verify that by console.log(promise). But because you chained then it returns another promise ..




                Return value [of then]



                A Promise in the pending status. The handler function
                (onFulfilled or onRejected) then gets called asynchronously (as soon
                as the stack is empty). After the invocation of the handler function,
                if the handler function:




                • returns a value, the promise returned by then gets resolved with the
                  returned value as its value;


                • doesn't return anything, the promise returned by then gets resolved with an undefined value;

                • throws an error, the promise returned by then gets rejected with the thrown
                  error as its value;


                ...




                This second point is what applies to your case. You can verify that by observing that
                Promise.reject().then(undefined, ()=>{})

                returns a promise that gets resolved with an undefined value.






                share|improve this answer






























                  1














                  The promise that is resolved is not the original promise, but the promise returned by then. The original promise is indeed rejected, and you can verify that by console.log(promise). But because you chained then it returns another promise ..




                  Return value [of then]



                  A Promise in the pending status. The handler function
                  (onFulfilled or onRejected) then gets called asynchronously (as soon
                  as the stack is empty). After the invocation of the handler function,
                  if the handler function:




                  • returns a value, the promise returned by then gets resolved with the
                    returned value as its value;


                  • doesn't return anything, the promise returned by then gets resolved with an undefined value;

                  • throws an error, the promise returned by then gets rejected with the thrown
                    error as its value;


                  ...




                  This second point is what applies to your case. You can verify that by observing that
                  Promise.reject().then(undefined, ()=>{})

                  returns a promise that gets resolved with an undefined value.






                  share|improve this answer




























                    1












                    1








                    1







                    The promise that is resolved is not the original promise, but the promise returned by then. The original promise is indeed rejected, and you can verify that by console.log(promise). But because you chained then it returns another promise ..




                    Return value [of then]



                    A Promise in the pending status. The handler function
                    (onFulfilled or onRejected) then gets called asynchronously (as soon
                    as the stack is empty). After the invocation of the handler function,
                    if the handler function:




                    • returns a value, the promise returned by then gets resolved with the
                      returned value as its value;


                    • doesn't return anything, the promise returned by then gets resolved with an undefined value;

                    • throws an error, the promise returned by then gets rejected with the thrown
                      error as its value;


                    ...




                    This second point is what applies to your case. You can verify that by observing that
                    Promise.reject().then(undefined, ()=>{})

                    returns a promise that gets resolved with an undefined value.






                    share|improve this answer















                    The promise that is resolved is not the original promise, but the promise returned by then. The original promise is indeed rejected, and you can verify that by console.log(promise). But because you chained then it returns another promise ..




                    Return value [of then]



                    A Promise in the pending status. The handler function
                    (onFulfilled or onRejected) then gets called asynchronously (as soon
                    as the stack is empty). After the invocation of the handler function,
                    if the handler function:




                    • returns a value, the promise returned by then gets resolved with the
                      returned value as its value;


                    • doesn't return anything, the promise returned by then gets resolved with an undefined value;

                    • throws an error, the promise returned by then gets rejected with the thrown
                      error as its value;


                    ...




                    This second point is what applies to your case. You can verify that by observing that
                    Promise.reject().then(undefined, ()=>{})

                    returns a promise that gets resolved with an undefined value.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 14 '18 at 12:48

























                    answered Nov 14 '18 at 12:38









                    Husam IbrahimHusam Ibrahim

                    3,048415




                    3,048415

























                        2














                        You can also handle Promises in "then-catch" fashion like below






                        let promise = new Promise(function(resolve, reject) {
                        setTimeout(() => reject('Error'));
                        });

                        promise
                        .then(result => console.log('Result ', result))
                        .catch(error => console.log('This is Error message -', error))








                        share|improve this answer


























                        • then accepts two arguments - one for success callback and the other for a rejection callback. Using it a valid way of dealing with Promise rejection.

                          – Szab
                          Nov 14 '18 at 12:03






                        • 1





                          Oh!! How did I miss that.. Damn!! Thank you @Szab..

                          – Nitish Narang
                          Nov 14 '18 at 12:06











                        • Edited my answer.. Thank you for the knowledge @Szab .. I never realised this...

                          – Nitish Narang
                          Nov 14 '18 at 12:08











                        • A promise should have exception handling mechanism. That is why we have .catch, the control goes to .catch when an exception is thrown from the promise body. if you call resolve the control goes to the first executor argument of .then and if you call reject, then it goes to the second argument of .then. So I think we should have both then and catch

                          – Parnab Sanyal
                          Nov 14 '18 at 12:56
















                        2














                        You can also handle Promises in "then-catch" fashion like below






                        let promise = new Promise(function(resolve, reject) {
                        setTimeout(() => reject('Error'));
                        });

                        promise
                        .then(result => console.log('Result ', result))
                        .catch(error => console.log('This is Error message -', error))








                        share|improve this answer


























                        • then accepts two arguments - one for success callback and the other for a rejection callback. Using it a valid way of dealing with Promise rejection.

                          – Szab
                          Nov 14 '18 at 12:03






                        • 1





                          Oh!! How did I miss that.. Damn!! Thank you @Szab..

                          – Nitish Narang
                          Nov 14 '18 at 12:06











                        • Edited my answer.. Thank you for the knowledge @Szab .. I never realised this...

                          – Nitish Narang
                          Nov 14 '18 at 12:08











                        • A promise should have exception handling mechanism. That is why we have .catch, the control goes to .catch when an exception is thrown from the promise body. if you call resolve the control goes to the first executor argument of .then and if you call reject, then it goes to the second argument of .then. So I think we should have both then and catch

                          – Parnab Sanyal
                          Nov 14 '18 at 12:56














                        2












                        2








                        2







                        You can also handle Promises in "then-catch" fashion like below






                        let promise = new Promise(function(resolve, reject) {
                        setTimeout(() => reject('Error'));
                        });

                        promise
                        .then(result => console.log('Result ', result))
                        .catch(error => console.log('This is Error message -', error))








                        share|improve this answer















                        You can also handle Promises in "then-catch" fashion like below






                        let promise = new Promise(function(resolve, reject) {
                        setTimeout(() => reject('Error'));
                        });

                        promise
                        .then(result => console.log('Result ', result))
                        .catch(error => console.log('This is Error message -', error))








                        let promise = new Promise(function(resolve, reject) {
                        setTimeout(() => reject('Error'));
                        });

                        promise
                        .then(result => console.log('Result ', result))
                        .catch(error => console.log('This is Error message -', error))





                        let promise = new Promise(function(resolve, reject) {
                        setTimeout(() => reject('Error'));
                        });

                        promise
                        .then(result => console.log('Result ', result))
                        .catch(error => console.log('This is Error message -', error))






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 14 '18 at 12:07

























                        answered Nov 14 '18 at 11:49









                        Nitish NarangNitish Narang

                        2,9401815




                        2,9401815













                        • then accepts two arguments - one for success callback and the other for a rejection callback. Using it a valid way of dealing with Promise rejection.

                          – Szab
                          Nov 14 '18 at 12:03






                        • 1





                          Oh!! How did I miss that.. Damn!! Thank you @Szab..

                          – Nitish Narang
                          Nov 14 '18 at 12:06











                        • Edited my answer.. Thank you for the knowledge @Szab .. I never realised this...

                          – Nitish Narang
                          Nov 14 '18 at 12:08











                        • A promise should have exception handling mechanism. That is why we have .catch, the control goes to .catch when an exception is thrown from the promise body. if you call resolve the control goes to the first executor argument of .then and if you call reject, then it goes to the second argument of .then. So I think we should have both then and catch

                          – Parnab Sanyal
                          Nov 14 '18 at 12:56



















                        • then accepts two arguments - one for success callback and the other for a rejection callback. Using it a valid way of dealing with Promise rejection.

                          – Szab
                          Nov 14 '18 at 12:03






                        • 1





                          Oh!! How did I miss that.. Damn!! Thank you @Szab..

                          – Nitish Narang
                          Nov 14 '18 at 12:06











                        • Edited my answer.. Thank you for the knowledge @Szab .. I never realised this...

                          – Nitish Narang
                          Nov 14 '18 at 12:08











                        • A promise should have exception handling mechanism. That is why we have .catch, the control goes to .catch when an exception is thrown from the promise body. if you call resolve the control goes to the first executor argument of .then and if you call reject, then it goes to the second argument of .then. So I think we should have both then and catch

                          – Parnab Sanyal
                          Nov 14 '18 at 12:56

















                        then accepts two arguments - one for success callback and the other for a rejection callback. Using it a valid way of dealing with Promise rejection.

                        – Szab
                        Nov 14 '18 at 12:03





                        then accepts two arguments - one for success callback and the other for a rejection callback. Using it a valid way of dealing with Promise rejection.

                        – Szab
                        Nov 14 '18 at 12:03




                        1




                        1





                        Oh!! How did I miss that.. Damn!! Thank you @Szab..

                        – Nitish Narang
                        Nov 14 '18 at 12:06





                        Oh!! How did I miss that.. Damn!! Thank you @Szab..

                        – Nitish Narang
                        Nov 14 '18 at 12:06













                        Edited my answer.. Thank you for the knowledge @Szab .. I never realised this...

                        – Nitish Narang
                        Nov 14 '18 at 12:08





                        Edited my answer.. Thank you for the knowledge @Szab .. I never realised this...

                        – Nitish Narang
                        Nov 14 '18 at 12:08













                        A promise should have exception handling mechanism. That is why we have .catch, the control goes to .catch when an exception is thrown from the promise body. if you call resolve the control goes to the first executor argument of .then and if you call reject, then it goes to the second argument of .then. So I think we should have both then and catch

                        – Parnab Sanyal
                        Nov 14 '18 at 12:56





                        A promise should have exception handling mechanism. That is why we have .catch, the control goes to .catch when an exception is thrown from the promise body. if you call resolve the control goes to the first executor argument of .then and if you call reject, then it goes to the second argument of .then. So I think we should have both then and catch

                        – Parnab Sanyal
                        Nov 14 '18 at 12:56











                        0














                        The final status of the promise is also rejected in your case.
                        Try console.log(promise);
                        It has nothing to do with error => alert(error)






                        share|improve this answer




























                          0














                          The final status of the promise is also rejected in your case.
                          Try console.log(promise);
                          It has nothing to do with error => alert(error)






                          share|improve this answer


























                            0












                            0








                            0







                            The final status of the promise is also rejected in your case.
                            Try console.log(promise);
                            It has nothing to do with error => alert(error)






                            share|improve this answer













                            The final status of the promise is also rejected in your case.
                            Try console.log(promise);
                            It has nothing to do with error => alert(error)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 14 '18 at 11:56









                            Monica AchaMonica Acha

                            33219




                            33219























                                0














                                The code you posted seems to work correctly both on Chrome and Firefox - the Promise was rejected as expected. reject function marks the Promise as rejected - you can then react upon that rejection either by passing a callback function into a second argument of then or by using the catch method. Both approaches are correct.



                                If you are encountering any unexpected behaviour, verify that you are not using some faulty polyfill that replaces the original Promise object.



                                Reference: Promise.prototype.then, Promise.prototype.catch






                                share|improve this answer




























                                  0














                                  The code you posted seems to work correctly both on Chrome and Firefox - the Promise was rejected as expected. reject function marks the Promise as rejected - you can then react upon that rejection either by passing a callback function into a second argument of then or by using the catch method. Both approaches are correct.



                                  If you are encountering any unexpected behaviour, verify that you are not using some faulty polyfill that replaces the original Promise object.



                                  Reference: Promise.prototype.then, Promise.prototype.catch






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    The code you posted seems to work correctly both on Chrome and Firefox - the Promise was rejected as expected. reject function marks the Promise as rejected - you can then react upon that rejection either by passing a callback function into a second argument of then or by using the catch method. Both approaches are correct.



                                    If you are encountering any unexpected behaviour, verify that you are not using some faulty polyfill that replaces the original Promise object.



                                    Reference: Promise.prototype.then, Promise.prototype.catch






                                    share|improve this answer













                                    The code you posted seems to work correctly both on Chrome and Firefox - the Promise was rejected as expected. reject function marks the Promise as rejected - you can then react upon that rejection either by passing a callback function into a second argument of then or by using the catch method. Both approaches are correct.



                                    If you are encountering any unexpected behaviour, verify that you are not using some faulty polyfill that replaces the original Promise object.



                                    Reference: Promise.prototype.then, Promise.prototype.catch







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 14 '18 at 12:02









                                    SzabSzab

                                    1,053517




                                    1,053517






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299500%2fwhen-promise-state-become-rejected-or-resolved%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Xamarin.iOS Cant Deploy on Iphone

                                        Glorious Revolution

                                        Dulmage-Mendelsohn matrix decomposition in Python