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?
2 Likes
  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.
2 Likes

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.

3 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.
3 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.
2 Likes
  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 Like
  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

1 Like

Code that runs outside the main thread. You need to wait for an response.

A function that you provide as argument to another function, that function can call it to send information back to the caller, via this “callback”.

Some tasks take a lot of time, for example: fetching data, waiting for a response of a transaction, a timer. In the meantime, it makes no sense to stop the code from executing and wait for a response before continuing.

It’s a delayed response in Javascript that can be resolved or rejected in a later time (asynchronous)

Waits for a promise to resolve and provides the resolved response.

1 Like

1. What is asynchronous code?
non-blocking code that will execute and return the result back when its done.
2. Describe what a callback function is
A function that will handle the response for code that was run asynchronously
3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
Asynchronous code is typically used when making calls to 3rd parties via APIs where the response time can vary. It is possible in some cases to make the call synchronous but it will pause the program while it is waiting for the result which would not work in the case of a game for example where the main loops needs to keep running.
4. What are Promises?
Promises allow us to create a single function that handles the future response object. This differs from callbacks where you need one function to make the call and another to handle the response.
5. What does the then() function do?
Waits for the promise object to return the result so we can handle the response

1 Like
  1. What is asynchronous code?

It’s code that is not in the imperative style. It usually involves I/O and waiting. A main thread takes a request, delegates it to some worker thread to handle why the initial thread then carries on with its work and does not block.

  1. Describe what a callback function is
    A callback function is the ‘hook’ that the delegated thread uses to return the result (that it eventually got) back to the original caller

  2. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…

in a node.js application there is one main thread which cannot be blocked (otherwise all other requests are blocked), so it takes each request and offloads it to worker threads (unlike java where the each request has a dedicated thread per request which blocks)

  1. What are Promises?
    A promise is a construct that encapsulates the eventual result of an asynchronous operation it can have 3 states waiting, done or failed

  2. What does the then() function do?
    Its way of defining the code that should only occur after the asynchronous call has completed

1 Like
  • What is asynchronous code?
    A set of code that runs on separate threads and not in sequence.
  • Describe what a callback function is
    Callback is a function that will be called when an asynchronous process ends
  • Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
    So we can run multiple tasks without waiting and pausing for feedback.
  • What are Promises?
    Promises are JavaScript’s implementation of asynchronous processes
  • What does the then() function do?
    The then() function set up the action when resolve or reject is called.
1 Like

1. What is asynchronous code?

Code that executes some task and continues to run until a result is reached. Meanwhile other synchronous or asynchronous tasks are executed without waiting.

2. Describe what a callback function is

A callback function is a function that executes once the result of an asynchronous task is complete.

3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…

Because we may have an application that responds to events in real-time and we would like those responses to be updated in real time without causing our program to freeze as in the case of synchronous code we can only have a single piece of code running at a time.

4. What are Promises?

Promises represent the future value of an asynchronous task carried out from before. Rather than use a callback to return a result, we can retrieve a Promise object representing the actual data.

5. What does the then() function do?

It waits for an asynchronous task to finish before the next task/tasks within .then() function is executed.

1 Like
  1. Asynchronous programming is a means of parallel programming in which a unit of work runs separately from the main application thread and notifies the calling thread of its completion, failure or progress. You may be wondering when you should use asynchronous programming and what are its benefits and problem points.

  2. A callback function is a function you provide to another piece of code, allowing it to be called by that code.

  3. Javascript is a single threaded language — this means Javascript can only do one thing at once. Which means waiting 30 seconds or as long it takes for the network request to be resolved.

  4. A promise is returned object to which you attach callbacks, instead of passing callbacks into a function.

  5. It performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output.

1. What is asynchronous code?
This means that you can do multiple requests at a time. instead of synchronous where you can just do it line for line.

2. Describe what a callback function is
A callback function is a function where you don’t have to wait to get data before doing another task/function.

3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
We need asynchronous code because then we can run multiple functions at the same time. Whereas with synchronous code you have to wait for every action/function that is called to get data this gives a bad UX.

4. What are Promises?
With promises we create a function that returns a promise object this can be a value that is going to be used in the future.

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

  1. Asynchronous code is when you scedule tasks to be done later rather then waiting for a trigger.
  2. A callback function is a function that you pass in to a function paramater.
  3. We need asynchronous code so that we dont have to wait forever until something is complete.
  4. A promise is a object that represents a future value.
  5. .then() is a function that specifies what happens after reject or resolve is called
1. What is asynchronous code?
Code that can perform tasks separately without  worrying about the other tasks running primarily on a single thread. This allows code to schedule task run in the future, while other tasks run before that task completes. 
2. Describe what a callback function is
A function passed as an argument to another function. 
3. Why do we need asynchronous code at all? Can’t we just replace it with synchronous…
Asynchronous code allows code to run multiple task at the same time, which isn't something that synchronous code allows. 
4. What are Promises?
A method of getting rid of callback hell by instead of using functions that accept input and a callback, then 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?

Is a method that returns a promise, which takes callback functions for success and failure cases of the Promise.

1 Like

1. What is asynchronous code?
Contrary to synchronous code, where line 2 is only executed after line 1 execution is finished, asynchronous code allows scheduling tasks to be run in the future, but line 2 runs before L1 completion.

2. Describe what a callback function is
It is a function that is passed into 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…
Asynchronous code allows processes to continue without depending on the completion of one specific task, while synchronous code is used for uninterrupted, third-party independent programming.

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

5. What does the then() function do?
The then() function method is used to specify what should happen once the future is received.

1 Like