mode 6 JS IV Flashcards

1
Q

What is AJAX?

A

-Asynchronous JavaScript and XML

Ajax describes the process of exchanging data from a web server asynchronously with the help of XML, HTML, CSS, and JavaScript. It just loads the data from the server and selectively updates some webpage parts without refreshing the page.

-It's a series of steps that will allow JS to perform tasks asynchronously
(btw, we'll be using JSON)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is asynchronous?

A
  • it means “non blocking”
    • a task that can be performed concurrently with other tasks
    • it won’t stop other threads from functioning at their own pace
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the steps to make an XMLHttp AJAX request?

A

We’re going to be using the XMLHttpRequest object (aka xhttp) to peform an AJAX request

STEP 1: create XMLHttpRequest Object this object allows us to make requests and get back data (response) in short, this is our data retriever object (it calls servers/apis)
STEP 2: create the callback function for readyState changes
STEP 3: create and open a connection - xhttp.open(httpMethod, url); OR
xhttp.open(httpMethod, url, boolean async?);
STEP 4: send the request
xhttp.send();

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

What are the readystates of XML HttpRequest object?

A

state 0: not initialized
state 1: server connection established
state 2: request received
state 3: processing request
state 4: complete, request finished and response is ready

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

What are the 3 different ways to perform an AJAXrequest?

A
  • XHTTP/XMLHttpRequest object
  • Fetch
  • Async-await
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can we turn a JSON string into a usable JavaScript object and vice versa?

A

JSON.parse() turns a JSON string into a usable JavaScript object

  • let poke = JSON.parse(xhttp.responseText);
  • console.log(poke);

There is also a JSON.stringify to turn a JavaScript object into a JSON

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

What is a promise in JS?

A

A promise is an asynchronous process that will wait for a response of some secondary process,
then trigger a “success” logic if the secondary process returned correctly OR it will trigger
a “failure” logic if the secondary process return incorrectly

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

Example of Promise

A
let mySecondaryProcessReturnBoolean = true;
let myGlobalState =25;
let myCustomPromise = new Promise(
    function (resolvingCallback, rejectingCallback){
    //the logic inside of this function will be your "secondary process'"

    if(mySecondaryProcessReturnBoolean){
        resolvingCallback("process RESOLVED!!!");
    }else{
        rejectingCallback("process FAILED!!!");
    }
} );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do we activate a promise?

A

The “.then()” function triggers the promise’s functionality

// myCustomPromise.then(myResolver, myRejection);
// myCustomPromise.then(myResolverTWO, myRejection);

or use a .catch() function to declare your rejection logic

myCustomPromise
//     .then(myResolver)
//     .catch(myRejection)
//     .finally(()=>{console.log("in the finally block")});

note: myResolver is my success function and myRejection is my failure callback function to be defined elsewhere.

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

Can we chain promises?

A

Yes we can because a promise….returns another promise

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

What is the Fetch

A

The Fetch API is a way to perform an AJAX request. It is an abstraction of XHttp Request

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

How do we use the Fetch API to perform an AJAX request?

A
fetch(`https://pokeapi.co/api/v2/pokemon/${pokeId}`)
    .then(
        function(daResponse){
            //this is a resolving function
        const convertedResponse = daResponse.json();

        // console.log("in the first .then()\t\t\t", convertedResponse);
        return convertedResponse;
    }
)
.then(
    function(daSecondResponse){
        //this is another resolving function
        console.log(daSecondResponse);
        console.log("Fetch is a thing; Gretchen was right.");
        ourDOMManipulation(daSecondResponse);
    }
)
.catch(
    (stuff)=> {console.log("this sucker exploded");}
)

}

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

What is the ASYNC FUNCTION

A

It is is a way to perform an AJAX request. It is an abstraction of XHttp Request

“await” is used in front of a promise, and will pause THIS line of logic, until the promise
has returned. In fact, hover over “fetch” in VS code to see the return type of fetch is a promise

    Btw, you CAN use the .catch() on the await fetch()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly