Promise Usage Tips
Just some personal observations while working extensively with asynchronous code and promises in particular.
In a promise
then()function, returned values are automatically resolved, andthrow's are automatically rejected.One extremely common anti-pattern I see from JavaScript developers is the misuse of
Promise.reject(...). When usingasync/awaitpromise rejection is analogous to throwing an exception, which should not be used for business logic. A good rule of thumb is tothroworrejectonly when assumptions made by your business logic have not been met, eg. null parameters and such.async/awaitis just a layer on top of promises. Addingasyncto a function that doesn’t need it (e.g. it doesn’t callawaitinside) will add a wasteful promise wrapper and a next-tick operation. This is because a promise always resolves on a next-tick andasyncmethods always return a promise.