JavaScript seen in React Flashcards

1
Q

What does destructuring do?

A

Makes it possible to unpack values from arrays or properties from objects into distinct variables.
Extract multiple properties from an object/array.

Example Code: var user = {
  name: 'Ray',
  handle: 'Ray@gmail.com'
  location: 'Bluffdale, UT'
}

var {name, handle, location} = user;

Order in this case wouldn’t matter as if I put handle before name, it would still pull out the email because of what I called it.

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

How is destructuring useful when dealing with function arguments?

A

We can set default values for any properties. So if there’s a value from the arguments we don’t want to use or define, we can give it a default value.
Example: Note - The reason the arguments are in a function is because he made a object and put in the argument values we care about as this is a good way of doing it

function fetchRepos({language = ‘All’, minStars=0, maxStars=’’, createdBefore=’’, createdAfter=’’})

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

What is destructuring in place?

A

If we have nested objects and want to pull it out, we could destructure it in place rather than create a new line. So let’s say we have an object that has a object and in that object we have a address object and we want to pull out the street property from it. We can destructure in place

const {name, id, address: {street}} = employee;

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

What arguments does a Promise take?

A

Callback functions of resolve and reject

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

What is a promise simply?

A

It is simply an object in JavaScript and is always in one of the three state: pending -initial state
fulfilled - operation success
rejected - meaning that the operation failed

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

Why use promises

A

One reason. Help deal with asynchronous code in a far more simpler way compared to callbacks

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

What are resolve/reject in promise?

A

The promise receives 2 arguments. Both are functions. Resolve changes state of promise from pending to fulfilled. Reject changes from pending to rejected. Cannot directly mutate the status of a promise. Can call resolve or reject. Usually called after a async operation.

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

When is then/catch method called.

A

then is when pending is fulfilled. catch is when pending is rejected.

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

What is the fetch API?

A

Make requests to a server

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

How do we use .then/.catch with an api call?

A

After the fetch api url we can attach the then which takes in the data as the argument and the catch that takes in the error.

fetch(‘https://pokeapi.co’).then((data) => console.log(data)).catch((err) => console.log(err));

The data isn’t necessarily what we want….Answered later

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

How to take json out of response of a then method?

A

Take the res and add res.json() at the end to return json. That returns a promise so we can chain on another then where we’re going to get actual data.

fetch(‘https://pokeapi.co’)
.then((res) => res.json())
.then((data)=>console.log(data))
.catch((err) => console.log(err));

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

Explain what an API is

A

When we need some data from a website, let’s say from google, our website needs to speak to Google so our server needs to speak to google server. This gives us a response which isn’t in HTML that our browser understands but rather JSON.

To summarize, when a company offers an API to their customers, it just means that they’ve built a set of dedicated URLs that return pure data responses — meaning the responses won’t contain the kind of presentational overhead that you would expect in a graphical user interface like a website.

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

Explain what async keyword is in JavaScript

A

We can declare any function in JavaScript as async and async functions always return promises. Inside of the async function we can write code that looks synchronous even if it isn’t.

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

How would you deal with an rejection in async function?

A

Simply throw an error inside of the async function.

async function oops() {
throw “you shouldn’t have invoked me!!”;
}

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

Explain the await keyword

A

We can use it inside of a async function. await pauses the execution of the async function. Can await any async operation returning a promise (i.e other async functions).

The await keyword waits for promise to resolve and extracts its resolved value. It then resumes the async function’s execution. Think of it like a pause button.

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

How do you handle error in async function?

A
If a promise is rejected using await, an error will be thrown. We can use try/catch statement to handle errors. 
try {
let url = "https://api{user}";
let response = await $.getJson(url)
} catch (e) {
   console.log("User does not exist");