Catching JavaScript errors with Promises
Warning: Please consider that this post is over 9 years old and the content may no longer be relevant.
If your JS function needs to return a Promise but doesn’t do any asynchronous activity, you may be tempted to use the static Promise.resolve() function rather than instantiating a new Promise object. Be aware that doing this will change the way javascript errors are dealt with.
Using the static method means normal JavaScript error handling will take place and your Promise catch() function won’t be called. E.g.
Will result in seeing the error ‘new error’ logged by the console. The promise then / catch functions are never called. Whereas:
Will allow the Promise to handle the error and pass it to the catch function, resulting in ‘there was an error’ in the console.