Assignment: Asynchronous Code

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

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

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
  1. Asynchronous code is code that runs on a separate code function while still processing on the main code.
  2. Its a function that will be called when an asynchronous process ends
  3. We need asynchronous code so that our application can process multiple functions in parallel without interrupting program flow on the main thread.
  4. 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. In asynchronous code you have several lines of code but they are not executed consecutively. For example a line of code begin to execute something but has to wait for a response. In the case of asynchronous code the program will not wait for the answer and only then execute the following lines of code, but instead the program jumps to the following lines and execute them. At some point the program comes back to the line of code when the response got back.

  2. Callback functions are used in asynchronous code. These callback functions are an argument for asynchronous function and are executed/called when the task of the async function is finished/response arrived.

  3. Not a good idea. Javascript is working single threaded. That means only one task can be performed at the same time. Imagine, you have a Dapp with several user which make several requests at the same time, everybode has to wait for the response and they cannot do anything else than waiting for it. So, everything on the Dapp is unresponsive.

  4. Promises are a way to deal with callbacks and asynchronous function with the aim of avoiding the callback hell. Instead of passing input and a callback to a function, we create a function which returns a promise objects. Inside this promise object there is the value which the developer wants to use in the future.

  5. When we are working with promises the developer determines inside the .then() function what will happen when the promise object is returned (which means the task is finished and some value e. g. a number is returned).

  1. What is asynchronous code?
    • synchrounous code: a new instructin can start if the previous instroction is finished
    • the execution of a asynchronous instruction must not be finished to start the next one
    • asynchronous does not mean multithreading

  2. Describe what a callback function is
    • a function inside a asynchronous function which will be called once tadk ist finisched

  3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
    • Since JS is single thread, purely synchronous programming would interrupt the websites and make them non-interactive

  4. What are Promises?
    • you can prevent a callback hell
    • the async function returns a promise object after execution which is intended to exist in the future

  5. What does the then() function do?
    • you can define what to do after getting the promise