JavaScript Set 5 Flashcards

1
Q

What does synchronous mean?

A

To be in a sequence such that every statement of the code is executed one by one.
A statement must wait for the earlier statement to complete

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

What does asynchronous mean?

A

To allow the program to be executed immediately without waiting for the earlier statement to complete

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

What is a Promise?

A

A JavaScript object that represents the eventual completion of an asynchronous operation and its resulting value.
It links the “producing code” and the “consuming code” together

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

How is a Promise like a proxy?

A

It allows you to associate handlers with an asynchronous action’s event on success or failure

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

What are the possible states of a promise?

A
  1. Fulfilled: Action related to a promise succeeding
  2. Rejected: Action related toa promise failing
  3. Pending: The initial state of the promise
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the restrictions on promises moving between states?

A
  1. A promise cannot go from fulfilled to rejected or vice versa
  2. A promise cannot go from fulfilled or rejected to pending
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the benefits of using Promises?

A
  1. Improves code readability
  2. Better handling of asynchronous operations
  3. Better flow of control definition in asynchronous logic
  4. Better error handling
  5. Avoid callback hell
  6. Better performance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What arguments does the Promise constructor take?

A

Takes one argument which is a callback function

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

What arguments does the Promise callback function take?

A
  1. Resolve function that is called if everything went well
  2. Reject function that is called if the callback function fails
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the 3 main Promise instance methods?

A
  1. then(): invoked when a promise is resolved or rejected
  2. catch(): deals with rejected cases only
  3. finally(): Called whether or not the promise is resolved or rejected
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the parameters accepted by the Promise’s then() method?

A

Takes 2 functions
1. Executed if the promise is resolved and a result is received
2. Executed if the promise is rejected and an error is received

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

What parameters are accepted by the catch() method?

A

Takes one function which is called when the promise is rejected

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

Since JavaScript already has try catch why does Promise need its own catch?

A
  1. try catch is used to handle synchronous errors but the Promise catch is used for asynchronous errors
  2. You often chain together Promises to perform a sequence of asynchronous tasks so you can go to the nearest catch function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What parameters are accepted by the finally() method?

A

Takes one function that is called when the Promise is resolved or rejected

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

What are the 3 main Promise static methods?

A
  1. all() waits for all promises to be resolved or one to be rejected
  2. any() returns a single promise that resolves with the value from the first Promise that resolved from an iterable of Promises
  3. race() waits until any of the promises are fulfilled or rejected
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What parameters are accepted by the all() method?

A

Takes an array of promises. It waits for all of the promises in the array to resolve or one of them to fail before calling the then() or catch() methods

17
Q

What parameters are accepted by the any() method?

A

Takes an array of promises. As soon as one promise fulfills it returns a single promise that resolves with the value from that finished promise

18
Q

What parameters are accepted by the race() method?

A

Takes an array of promises. It will wait until any one of the promises is fulfilled or rejected

19
Q

What is the difference between an instance method and a static method?

A
  1. An instance method is specific to an object where a static method are common to every object
  2. Instance methods are always accessed by the instance object where static methods are always accessed with the original object
20
Q

What is node.js

A

Allows you to run JavaScript on a web server. It can generate dynamic page content; can create, open, read, write, delete, and close files on the server; can collect form data; can add, delete, and modify data in a database

21
Q

How do you run JavaScript from the command line with node?

A

Using the node keyword:
node FileName.js

22
Q

What does REPL stand for and what does it allow you to do?

A

Stands for Read, Evaluate, Print, Loop
Allows you to enter JavaScript commands, evaluate them, print results, and loop to enter another command

23
Q

How do you access modules that are not built-in to node.js?

A

Using the require keyword
Ex:
var mod = require(“module”);

24
Q

What is the HTTP module?

A

Allows node.js to transfer data using HTTP protocol. Makes node.js into a web server

25
Q

What is the syntax for creating a server with the http module and having it listen on a particular port?

A

http.createServer(function (req, res) {
// function code
}).listen(8080)