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?
1 Like

#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.
1 Like

#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.

2 Likes

#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.

2 Likes

#5
  1. What is asynchronous code?
    A set of code running without following the sequence.
  2. Describe what a callback function is
    we can create a callback function that we pass in to an asynchronous function, which will be called once the task is completed.
  3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
    Because JS only has a single thread, If I use the synchronous function, then I may not be able to do anything while the previous function is running.
  4. What are Promises?
    we make a function that returns a promise object, that is, an object representing a value that is intended to exist in the future.
  5. What does the then() function do?
    To set up follow up action when resolve or reject is called.
2 Likes

#6
  1. What is asynchronous code?
    Asynchronous code is code that runs on a seperate thread without stopping processing on the main thread.
  2. Describe what a callback function is
    A callback function is a function that will be called when an asynchronous process ends.
  3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
    We need asynchronous code so that our application can process multiple functions in parallel (such as loading several images, or processing data) without interrupting program flow on the main thread.
  4. What are Promises?
    Promises are JavaScript’s implementation of asynchronous processes (since JavaScript is not multithreaded) and represent a future state of a task.
  5. What does the then() function do?
    The .then() functions enables the programmer to “pipeline” a series of promises so that they execute in sequence and help avoid callback hell.
2 Likes

#7

Asynchronous Code

  1. It's a piece of code where you don't need to wait for this code to be completely executed (single treaded in JS) So the program can shift it's attention to other tasks in the meantime.

  2. Callbacks are also functions where you don't need to wait until a certain task is completed in order to continue with other code

  3. We need asynchronous code to shift your programs attention to other tasks while waiting for a response (ex. when clicked a button or after some timeout or AJAX requests)

  4. Promises represent a value wich may be available now or in the future (or never) Promises will produce a completion or a failure of a synchronous operation and it's value. It's also much cleaner than a 'callback hell'!

  5. The Then() function specifies what should happen when a callback function is Resolved or Rejected

Greetz Ziomanzo

1 Like