Asynchronus JavaScript Flashcards
What is asynchronous execution?
asynchronous execution is where we invoke another method/process but continue execution in the code that called it
What is the Microtask Queue?
an internal queue used by JavaScript engines
determines what task and what actions to execute next
Event Loop
the event loop is where tasks that are pulled from the microtask queue are executed
Callback Functions
a callback function is a function that’s passed as an argument to another function to be executed when the first function returns
callback functions are frequently used with asynchronous execution in JavaScript
Promises
a promise is a proxy for a value that will eventually become available
the promise eventually moves to a resolved or rejected state, invoking either the callback function(s) passed to then or catch, respectively
What changes are made to a Promise when it is settled?
state initially has a pending value that changes to fulfilled when the resolve callback is invoked or rejected when the reject callback is invoked
result is initially undefined, then changes to the value returned from the promise when the resolve callback is invoked or the error when the reject callback is invoked
How do we Consume Promises?
we can consume our promises by using “then”, “catch”, and “finally” methods
finally method allows us to execute code regardless fo whether our promise has been resolved/rejected
Fetch API
the Fetch API is a JavaScript API that we can use in browsers to easily make callouts to external services
when making such a callout, we’ll invoke the fetch() method
Async/Await
- async/await is built on promises (it’s just an abstraction that makes for a more readable way to work with promises)
- functions that begin with the async keyword (which is placed before the function keyword in the function declaration) always return a promise
- any return statements in an async function that don’t explicitly return a promise will have their values wrapped in a resolved promise
- the await keyword makes the JavaScript engine suspend function execution until a promise settles and returns a result
- the await keyword can only be used inside async functions
setTimeout()
- will invoke the callback function passed as the first argument after the amount of time specified has elapsed
setInterval()
- will invoke the callback function passed as the first argument every interval period