JS Fundamentals 3 - ES6 Async Flashcards
Pyramid of Doom
Deeply nested async code
Built from dependent async fns that can potentially all have errors.
Avoid with: Promise, .then, and try/catch
Promise syntax
roughly:
new Promise( (resolve, reject) =>
try {
resolve(‘abc’)
catch (e){
reject(e)
Async/Await
async keyword allows the fn to have await expressions, which make promise-returning fns behave like synchronous code
Promise methods - value passed in
Promise.resolve(value) - creates a promise that resolves w/the value passed in.
Promise.reject(value) - creates a promise that rejects w/the reason passed in.
Used when you want to start a Promise chain OR when you want to turn a value or error into a Promise. Also useful for testing.
Promise methods - Concurrency
Not true concurrency, but control switches making it appear they are running at once
All of these take an iterable of promises as input:
.all - fulfills when ALL fulfill, rejects when ANY reject
.allSettled - fulfills when ALL are settled
.any - fulfills when ANY fulfill, rejects when ALL reject
.race - fulfills when ANY fulfill, rejects when ANY reject
Breakpoints
Sources tab, click on line num.
Code pauses, allows you to examine the variables, execute commands in the console, etc.
Watch has current values for any expressions you add, Call Stack has nested calls chain, Scope has current variables.
Add debugger;
to code, and it functions the same way.
Breakpoint step actions
“Step” = runs the next command, ignores async
“Step over” = run next command but don’t go into a fn - good when you aren’t interested to see what happens in that fn call
“Step into” = similar to step but will run async
“Step out” = continue till the end of the current fn - stops at last line of current fn