Assignment: Asynchronous Code


#1

Welcome to this assignment. Please answer the following questions from the lecture. Feel free to ask questions here as well if you need the help.

  1. What is asynchronous code?
  2. Describe what a callback function is
  3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
  4. What are Promises?
  5. What does the then() function do?

#2
  1. Asynchronous code which doesn’t execute line by line instead it can be waiting for some task and keeps on doing other tasks.
  2. A callback function is the one in which is passed as a parameter to the other function.
  3. We use asynchronous functions because sometimes we have to wait for some response from a function and we don’t know when will we get it. We can replace it with synchronous code but it freezes our application until we get the response from the function.
  4. A promise is an object returned by a function representing the value that is intended to exist in the future.
  5. then() specifies what should be done once the function has returned the promise object.

#3

1. What is asynchronous code?
Asynchronous code is code that can be executed to plan future executions often while waiting for a response from other code or after a specific time has passed.

2. Describe what a callback function is
A callback function is a function passed to the asynchronous function such that the callback can be executed after the asynchronous code is finished.

3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
When only using synchronous functions the user may have to wait with unresponsive user interfaces while the single thread code is synchronously waiting. This will provide a bad user experience.

4. What are Promises?
A promise is a function which returns an object representing a value that is intended to exist in the future.

5. What does the then() function do?
.then() waits for the response from the previous function and then executes the following code.


#4
  1. Asynchronous code executes tasks in order needed/called independent of preceding tasks completing… which emulates running multiple tasks at once, not in sequential order like Synchronous code where users would need to wait for each task to complete before executing the next task.

  2. Callback function is a function passed to another function as an argument which is executed first and returns as argument.

  3. Asynchronous code provides a better user experience and simplifies code as noted in 1.

  4. A Promise will return a value when completed… either a resolved value or rejected with reason.

  5. Then() is the method of Promise that executes once the Promise value is returned.