Mod 5 Flashcards
How are synchronous functions executed and how does that different from the execution of asynchronous functions?
A synchronousfunction runs to completion before any other code is executed in the program.
For example, when one function X calls another function Y, nothing happens in X until the function Y completes its execution and returns. As opposed to this, asynchronous functions are non-blocking. As soon as an asynchronous function is called, the next line of code in the calling function can be executed. For example, consider a function X calls an asynchronous function Z. As soon as Z is called, the next line in X can be executed and X does not have to wait for Z to complete. The asynchronous function Z is executed by the JavaScript engine separate from the main thread executing the function X.
Why should we avoid calling long running synchronous functions in our JavaScript code?
Such functions prevent the user from interacting with the webpage while they are occurring and cause frustration.
What is a JavaScript Promise?
A Promise is a JavaScript object that represents the result of an asynchronous operation whose results may not be available as yet. The core idea is this:
- The asynchronous function returns a Promise object.
The initial state of this Promise object is pending meaning it does not yet have a result.
- When the asynchronous function successfully finishes, it fills in the Promise object with the result and sets its state to fulfilled. The promise is now said to have been fulfilled. The Promise is said to have resolved to this result value.
- When the asynchronous function fails due to an error, it will not produce a value. In this case, the state of the Promise object is set to rejected. The promise is said to have been rejected.
- Once the state of the Promise objects is set to either the fulfilled or rejected, it is said to have been settled.
What are then() and catch() methods?
- Using the then() method on the promise, we provide a function that will be called when the promise returns a result, i.e., it is fulfilled. If the function provided to then() returns another promise, we can process its result by calling the then() method again.
- Using the catch() method on the promise, we provide a function that will be called when the promise fails, i.e., it is rejected.
What does it mean for a promise to be settled?
It means the promise has either been fulfilled and resolved to value or rejected and set to rejected.
When does it mean for a promise to be fulfilled and resolve to a value?
When the asynchronous function successfully finishes, it fills in the Promise object with the result and sets its state to fulfilled. The promise is now said to have been fulfilled. The Promise is said to have resolved to this result value.
How can you chain promises?
You can line up a series of .then() methods so that one promise fulfills and triggers another function that creates a promise and so on.
How can you obtain the result for a fulfilled promise?
You can use .then() method to capture the promise when it has resolved to a value and call other functions.
How can you use the await keyword to wait for a promise to be settled?
When assigning a promise to a variable, you can use the await keyword to pause the assignment until the promise has been resolved. **Await* can only be used in an asynchronous function, as the await only pauses the assignment and allows the main thread to continue processing.
What are the different ways a calling function can get a value from an async function?
An async function is executed asynchronously which means that the call to the async function returns immediately. An async function always returns a promise. If the calling function wants to use the value returned by the async function, then there are 2 options:
- Call the then() method on the returned promise to get the result.
- Use await keyword when calling the function and wait for the promise to settle.
But since the await keyword can only be used in an async function, this means that we will need to add the async keyword to the calling function as well!
Why is the concept of modules useful?
Modules allow us to use code from other files. This gives access to large libraries so we don’t reinvent the wheel or rewrite our own code. We all love to save time!
How do we export features from ES modules?
We use the export and import keywords:
export const COUNTRY = ‘USA’;
export default function readEntity(…){ … }
import readEntity, {COUNTRY} from ‘./model.mjs’
What is meant by named exports and what are default exports in ES modules?
Named exports are classes, functions, and variables that have the export keyword and are imported in curly braces or with the export all (*) command. The defaul export in an ES module has the export default … keyword and does not need curly braces, meaning it can be renamed in the import without the word ‘as’.
What is the use of the require function when using CommonJS modules?
The CommonJS reuire function works like import and assigns the exports of a module to a variable as properties.
Most middleware functions take what three arguments? What other argument does Error-handling middleware take?
- A request object,
- A response object, and
- A next() function.
- An error object