2

I'm studying the differences between return Promise.reject , and, throw new Error, looks similar , here the catch code:

} catch (err) {
  return Promise.reject(err);
  //throw new Error(err);
}

for throw new Error I see only is added 'TypeError'

sometimes i use, inside async function, and looks works all fine too..

throw(err);

    {
        "data": {
            "userlogs": null
        },
        "errors": [
            {
                "message": "TypeError: Cannot read property 'findAll' of undefined",
                "locations": [
                    {
                        "line": 2,
                        "column": 3
                    }
                ],
                "path": [
                    "userlogs"
                ]
            }
        ]
    }

with Promise the message is cleaner:

    {
        "data": {
            "userlogs": null
        },
        "errors": [
            {
                "message": "Cannot read property 'findAll' of undefined",
                "locations": [
                    {
                        "line": 2,
                        "column": 3
                    }
                ],
                "path": [
                    "userlogs"
                ]
            }
        ]
    }

is there and advise about that? i'm using await for call the functions, it's better to use Promise.reject in that scenary, and throw new Error is used for any case?

Vikas Yadav
  • 3,094
  • 2
  • 20
  • 21
DDave
  • 1,400
  • 3
  • 16
  • 33
  • 1
    The important difference is that the `throw` constructs a new `Error` instance, while the `Promise` version does not. I suspect that if you returned `Promise.reject(new Error(err));` or changed the `throw` to just `throw err;` then the behaviors would be identical. – Pointy Dec 11 '17 at 18:05
  • See this question [https://stackoverflow.com/questions/33445415/javascript-promises-reject-vs-throw] may help you – Yassine Qoraiche Dec 11 '17 at 18:08
  • thanks four your responses...I've changed everything to Promise.reject – DDave Dec 11 '17 at 23:41

0 Answers0