javascript Flashcards

1
Q

how do we handle asynchronous code in javascript

A

We have a few different ways to approach asynchronous code, callbacks for example, but Other concepts are async/await and Promises. Javascript is single-threaded so has no ability to manage threads the way C# and C++ do. Javascript runs synchronously.

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

what is a promise in javascript?

A

A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action’s eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.

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

What are the states that a promise can be in?

A

Pending: a state where the promise is neither rejected nor fulfilled. (this is the state it is in when we first call it)
Fulfilled: a state where all’s well and a resolved value can be used by our code.
Rejected: a state where something went wrong and there is an error that needs to be dealt with.

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

when is a promise most often used ?

A

when making API calls we use the helper object called a promise to inform the browser that the second, async task has finished.

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

What does promise handling look like? include then and catch

A
timeMachine()
  .then(parseTime)
  .then(timePassed => {
    console.log(timePassed); --> OUTPUT: DOES NOT RUN
  })
  .catch(err => {
    console.log(err); --> OUTPUT: ​​​​​[Error: ms is less than 1 second promise rejected!]​​​​​
  });
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why do we have asynchronous code?

A

Things aren’t all happening at the same time, they are actually being broken down into smaller parts, time-slice is used to do bits of each task. Threads are built into hardware to allow software developers to code. A thread is a chunk of memory and other state information about an executable thats running.

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

What does synchronous code mean?

A

synchronous means that each thing has to be complete before the next can start. Ie in a function call, everything in that function call must be complete before the next function call can begin running.

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

What is asynchronous code?

A

asynchronous code is the ability to do 2 things at the same time. We use asynchronous code to allow the browser to keep executing code while something else is happening- usually a call to an external API.

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

what is callback hell?

A

nesting callback functions to deliver asynch functionaility, its aweful and promises are a way to resolve this

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

What is async await

A

functionaly it is no diferent to a promise, however it looks more synchronous in the way its written.

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