HOMEWORK: Continue working on your project…
PREP FOR NEXT SESSION: https://github.com/HackYourFuture-CPH/JavaScript/blob/main/javascript3/week3/preparation.md
Why do we use Promises?
When do we use Promises?
Resources & exercises
Follow these rules and you’ll be fine 🤪
catch
then
,
even if it’s just return Promise.resolve()
Handy tips
Promise.all([...])
if you’re waiting for multiple ones.catch(console.log.bind(console))
as a safe default, until you come up with a better way to catch your errorsPromise.resolve() .then(() => { // Your async operation ... return asyncOperation() }) .catch(console.log.bind(console))
Why all these rules? Debugging issues in Promise chains can be very frustrating and these tips will help you avoid most pitfalls
Resources & exercises
A different syntax for dealing with Promises and async code
It lets us treat asynchronous code like it was synchronous code
No freaky rules, you can start using it now
Handy tips
try-catch
whenever possible to make your code more stableasync
keyword is required in front of the closest function to wherever you use the await
keywordasync
keyword return a Promise and need to be await
ed (however, if you don’t care about the result, you can also omit it and let the function “run in the background”)new Promise()
Resources & exercises