AsyncJS, HTTP requests, AJAX Flashcards

1
Q

What is the call stack?

A

Callstack is a mechanism the JS interpreter uses to keep track of its place in a script that calls multiple functions.
This is how JS knows what function is ucrrently being run and what functions are called from within that function.

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

What does it mean that JS is Single Threaded?

A

It means, that at any given point in time JS is running at most one line of JS code.

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

How do asynchronous callbacks work?

A

JavaScript is SingleThreaded, however the browsers are written in different languages (usually C++)/
Browsers come with WEB APIs that are able to handle certain tasks in the background.
The JS call stack recognizes these WEB API functions and passes them off to the browser to take care of.
Once the browser finishes those asks, they return and are pushed onto the stack as a callback.

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

What are promises?

A

Promise is an object representing the eventual completion or failure of an asynchronous operation.
Usually a promise has to be resolved or rejected.

You declare a promise with new keyword.

const willGetYouADog = new Promise((resolve,reject)=>{
   if () resolve()
   else reject()
}

You can use then and catch methods to handle promises. The then method is executed immediately after your promise is fulfilled with resolve. catch is the method used when your promise has been rejected. It is executed immediately after a promise’s reject method is called.

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

What is the pattern to make promises?

A

It is usual to create a function that returns a promise, instead of creating a promise and saving it to a variable.
This pattern is especially useful, when we are trying to chain promises to go further in the data structure that we are browsing and try to extract this data.

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

Whats an XMLHttpRequest?

A

Is the ‘original’ way of sending requests via JS (at that time xml was go too). We can use XHR to fetch any sort of data from an API.

  • Does not support promises (lots of callbacks if there are many nested requests)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the method to parse JSON file to JS object?

A

JSON.parse(data);

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

What is Fetch API

A

IT’s the newer way of making requests via JS.
It supports promises.

fetch() requires one argument (the URL to the API) You get in return the response object, the content of that response object is included as a ReadableStream (a stream is a way of accessing data, that can be super large)

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

What’s async keyword?

A

Async & await keywords are syntatical sugar for promises.
Async functions always return a promise.
If the function returns a value, the promise will be resolved with that value.
If the function throws an exepction the promise will be rejected.

We use async keyword to declare an async fucntion or async function expression.

const getPeople = async () => {
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What’s the purpose of await functions?

A

We put the await keyword inside of an async function and it will pause the execution of the function while it waits for the promise to be resolved.

const response = await fetch('https://swapi.dev/api/people/')
await keyword stops javascript from assigning the value to the variable  until the Promise is resolved.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you make requests with HTML forms?

A

When it comes to sending form data with http requests, we can either

1) use built in html forms
2) send calls directly to the api with fetch (or some ajax library)

In order to send our form data automatically

i) we need to specify that endpoint with action attribute
like: action=’/submit-user-data’
ii) specify the HTTP method
iii) specify the names of the inputs, because, they will be used to identify the data.
iv) on the server side, we need to setup a new route, strictly for updating data, and update it using our mongoose.Schema, for that we need to use in our app.use(express.urlencoded()) (in order to have access to req.body)

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