Asynchronous code Flashcards

1
Q

What are asynchronous functions?

A

Functions that can happen in the background while rest of your code executes

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

What kind of trouble can we run with this piece of code?

const getData = function() {
// go fetch data from some API…
// clean it up a bit and return it as an object:
return data
}

const myData = getData()
const pieceOfData = myData[‘whatever’]

A

when we try to extract pieceOfData out of the returned data, the function getData() will most likely still be fetching, so myData will not be the expected data, but will be undefined. Sad.

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

What can solve the problem, where we need to tell our code to wait until the data is done fetching to continue?

A

Promises

const myData = getData() // if this is refactored to return a Promise…

myData.then(function(data){ // .then() tells it to wait until the promise is resolved
const pieceOfData = data[‘whatever’] // and THEN run the function inside
})

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

JS is a single-threaded language. What does it mean?

A

It means that JS has only call stack that is used to execute the program

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

What is event loop’s job?

A

Event loop job is to look at the call stack and look at the task queue. If the stack is empty, it takes the first think from the task queue and pushes it into the stack

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

What is an API?

A

API (application programming interface) is a software intermediary that allows two applications to talk with each other.

Imagine a waiter who is between the customer and the chef.

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

How is access to an API restricted?

A

You have to request an API key which is unique to you. For that you need to make an account.

Services can correlate your API key to your requests of their data, including how much and how often you are requesting it.

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

How do you fetch and extract data from an API?

A

You can do that using promises. At first you’ll use fetch(), after that with .then() function

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

How do you fetch and extract data from an API?

A

You can do that using promises. At first you’ll use fetch(), after that with .then() function

an example:

fetch(‘https://api.giphy.com/v1/gifs/translate?api_key=YOUR_KEY_HERE&s=cats’, {mode: ‘cors’})
.then(function(response) {
return response.json();
})
.then(function(response) {
console.log(response);
});

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

Why might your API request be blocked by the browser, and how might you fix this?

A

For security reasons, by default, browsers restrict HTTP requests to outside sources (which is exactly what we’re trying to do here). With fetch, you are able to easily supply a JavaScript object for options. It comes right after the URL as a second parameter to the fetch function:

fetch(‘url.url.com/api’, {
mode: ‘cors’
});

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

What does the async keyword do?

A

It lets the JS engine know that you are declaring an asynchronous function. This is required if you want to use await inside any function

When function is declared as async, it automatically returns a promise

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

What does the await keyword do?

A

It tells JavaScript to wait for an asynchronous action to finish before continuing with the function. It’s like a “pause until done” keyword

The await keyword is used to get a value from a function where you would normally use .then(). Instead of calling .then() after the asynchronous function, you would simply assign a variable to the result using await.

Functions always returns a promise

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

How to handle errors with async keyword?

A

You can simply call a function and add .catch() method to the end.

But if you want to handle errors directly in the async functions, you can use try/catch methods.

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

What is a Promise?

A

Promise is an object that might produce a value sometime in the future

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