Mod 5 Flashcards

1
Q

How are synchronous functions executed and how does that different from the execution of asynchronous functions?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why should we avoid calling long running synchronous functions in our JavaScript code?

A

Such functions prevent the user from interacting with the webpage while they are occurring and cause frustration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a JavaScript Promise?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are then() and catch() methods?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does it mean for a promise to be settled?

A

It means the promise has either been fulfilled and resolved to value or rejected and set to rejected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When does it mean for a promise to be fulfilled and resolve to a value?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you chain promises?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you obtain the result for a fulfilled promise?

A

You can use .then() method to capture the promise when it has resolved to a value and call other functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you use the await keyword to wait for a promise to be settled?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the different ways a calling function can get a value from an async function?

A

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!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why is the concept of modules useful?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do we export features from ES modules?

A

We use the export and import keywords:

export const COUNTRY = ‘USA’;
export default function readEntity(…){ … }

import readEntity, {COUNTRY} from ‘./model.mjs’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is meant by named exports and what are default exports in ES modules?

A

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’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the use of the require function when using CommonJS modules?

A

The CommonJS reuire function works like import and assigns the exports of a module to a variable as properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Most middleware functions take what three arguments? What other argument does Error-handling middleware take?

A
  1. A request object,
  2. A response object, and
  3. A next() function.
    - An error object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is meant by middleware pipeline?

A

Creating a series of middleware functions that call next() which triggers the next middleware function for that URL. Note that after the first response is sent, other middleware can still be activate but they will not send further response.

17
Q

What is the use of the function next in the middleware pipeline?

A

next() causes the next middleware function for a specific request to activate. It is essential for pipelining middleware whether we use multiple route handlers or use one route handler with multiple middleware functions as its parameters.

18
Q

What is Express middleware?

A

A middleware is simply a function that Express applies to an HTTP request in our Express programs. We can think of an Express program as a pipeline of middleware functions which are applied to an HTTP request. These functions can be used for logging content, dynamic responses, manipulating data, and sending responses.

Middleware is easy to remember because it is code that executes in the middle of the HTTP request and response (although it can technically occur after a response is sent too)

19
Q

How can we add middleware using the app.METHOD ans app.use APIs?

A

app.use occurs for a request to a specific URL while app.METHOD (like app.get()) occurs only for a specific type of request to a specific URL. They can both use named middleware functions and pipeline with each other as long as they call next().

20
Q

What is error-handling middleware and how does it differ from other middleware?

A
  • Error-handling middleware has 4 parameters (error, request, response, next()).
  • Error handling middleware should be placed after all the route handlers and other non-error handling middleware. Thus if an error handling middleware does not call next(), it should return a response.
  • It can be used to handle any errors from previous middleware