MDN docs study Flashcards

studying from MDN docs on JS

1
Q

What is the Fetch API?

A

The Fetch API provides an interface for fetching resources (including across the network).

The Fetch API uses Request and Response objects (and other things involved with network requests), as well as related concepts such as CORS and the HTTP Origin header semantics.

For making a request and fetching a resource, use the fetch() method. It is a global method in both Window and Worker contexts. This makes it available in pretty much any context you might want to fetch resources in.

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

What is the fetch() method?

A

For making a request and fetching a resource, use the fetch() method. It is a global method in both Window and Worker contexts. This makes it available in pretty much any context you might want to fetch resources in.

he fetch() method takes one mandatory argument, the path to the resource you want to fetch.

It returns a Promise that resolves to the Response to that request — as soon as the server responds with headers — even if the server response is an HTTP error status.

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

What are the Fetch API Interfaces?

A

fetch()
Headers
Request
Response

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

How is the Fetch API different from the XMLHttpRequest API?

A

Unlike XMLHttpRequest that is a callback-based API, Fetch is promise-based and provides a better alternative that can be easily used in service workers.

Fetch also integrates advanced HTTP concepts such as CORS and other extensions to HTTP.

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

What does a basic fetch() request look like?

A

The simplest use of fetch() takes one argument — the path to the resource you want to fetch — and does not directly return the JSON response body but instead returns a promise that resolves with a Response object.

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

How do you extract JSON body content from the Response object returned by fetch()?

A

The Response object, in turn, does not directly contain the actual JSON response body but is instead a representation of the entire HTTP response.

So, to extract the JSON body content from the Response object, we use the json() method, which returns a second promise that resolves with the result of parsing the response body text as JSON.

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

Explain Fetch in a nutshell.

A

At the heart of Fetch are the Interface abstractions of HTTP Requests, Responses, and Headers, along with a fetch() method for initiating asynchronous resource requests. Because the main components of HTTP are abstracted as JavaScript objects, it is easy for other APIs to make use of such functionality.

Service Workers is an example of an API that makes heavy use of Fetch.

Fetch takes the asynchronous nature of such requests one step further. The API is completely Promise-based.

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

What are some of the settings you can set in the fetch() method’s second param (init)?

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

How do you process a text file line by line from a fetch() response?

A

The chunks that are read from a response are not broken neatly at line boundaries and are Uint8Arrays, not strings. If you want to fetch a text file and process it line by line, it is up to you to handle these complications. The following example shows one way to do this by creating a line iterator (for simplicity, it assumes the text is UTF-8, and doesn’t handle fetch errors).

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

How do you check if a fetch() was successful?

A

A fetch() promise will reject with a TypeError when a network error is encountered or CORS is misconfigured on the server-side, although this usually means permission issues or similar — a 404 does not constitute a network error, for example. An accurate check for a successful fetch() would include checking that the promise resolved, then checking that the Response.ok property has a value of true. The code would look something like this:

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

What are common properties of the Response instances returned when fetch() promises are resolved?

A

The most common response properties you’ll use are:

  • Response.status — An integer (default value 200) containing the response status code.
  • Response.statusText — A string (default value “”), which corresponds to the HTTP status code message. Note that HTTP/2 does not support status messages.
  • Response.ok — seen in use above, this is a shorthand for checking that status is in the range 200-299 inclusive. This returns a boolean value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What types can the body data of a Response instance be?

A

ArrayBuffer
TypedArray (Uint8Array and friends)
DataView
Blob
File
String, or a string literal
URLSearchParams
FormData

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

How do you upload a file using fetch()?

A

Files can be uploaded using an HTML <input></input> input element, FormData() and fetch().

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

How do you upload multiple files using fetch()?

A

Files can be uploaded using an HTML <input></input> input element, FormData() and fetch().

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

How do you supply your own request object to fetch()?

A

Instead of passing a path to the resource you want to request into the fetch() call, you can create a request object using the Request() constructor, and pass that in as a fetch() method argument:

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