Asynchronous Programming - Reading Assignment

Excellent answers sir.

However, wanted to add a comment on this statement.

Multiple callbacks are not necessarily bad. It is only when a large number of callbacks are stacked one after the other creating a complex structure that it becomes a “call back hell”.

Happy learning. :slight_smile:

2 Likes

Hello sir,
In your answer you got Asynchronous and synchronous functions swapped.

Synchronous functions are the ones that are executed one after the other whereas asynchronous functions allow rest of the program to continue after it’s called. Basically asynchronous functions do not make your program wait for it to finish executing.

Please let me know if you have any questions.

Happy learning :slight_smile:

2 Likes
  1. Synchronous functions focuses on its current task and will not begin executing the next task until the first task’s completion. Asynchronous functions are executed and completed at a later point in time, being active alongside all the other orderly executed synchronous tasks.
  2. A callback is a function that is executed but finished in some form in the browser. The function can sometimes become quite complex with many callbacks doing different things. It then becomes a callback hell.
  3. By using promises or extending the runtime
1 Like

1. What is the difference between synchronous and asynchronous functions? Synchronous functions are executed in sequence, where each task begins once the previous one has finished. On the other hand asynchronous functions can be initiated and then put aside until a result is available to be operated on, this means that other tasks can continue whilst we wait for the result.

2. What is callback hell? Callback hell occurs when you have an excessive number of callbacks nested within one another, making the code overly complex to manage.

3. Which technique can help us solve callback hell? Promises allows us to chain callbacks and deal with errors, which helps make the code easier to read and manage.

1 Like

Oh…yes i swapped them up :sweat_smile:

Thanks for making me aware of this.

1 Like
  1. What is the difference between synchronous and asynchronous functions?

First, we need to clarify that JavaScript is a language that can care of one task at a time, therefore is called a single-threaded programming language.

With this in mind we can say that synchronous functions are those functions that are followed one by one and the language won’t perform any other task until it hasn’t fully completed the previous one.

Asynchronous functions can be initiated and then put aside until a later date while another task can be running along without the need of waiting for completion of the other function.

  1. What is callback hell?

A callback hell occurs when there are multiple asynchronous functions nested within others simultaneously waiting for the next one to callback, this can lead to really complex and undesirable situations.

  1. Which technique can help us solve callback hell?

A common pattern to deal with callback hells is the use of promises, these enable us to chain callbacks and deal with errors

1 Like
  • What is the difference between synchronous and asynchronous functions?
    Synchronous basically means that you can only execute one thing at a time. Asynchronous means that you can execute multiple things at a time and you don’t have to finish executing the current thing in order to move on to next one.
  • What is callback hell?
    Callback hell is any code where the use of function callbacks in async code becomes obscure or difficult to follow. Generally, when there is more than one level of indirection, code using callbacks can become harder to follow, harder to refactor, and harder to test.
  • Which technique can help us solve callback hell?
    Declare your functions beforehand

One of the best ways to reduce code clutter is by maintaining better separation of code. If you declare a callback function beforehand and call it later, you’ll avoid the deeply nested structures that make callback hell so difficult to work with.

  1. Synchronous functions are executed in a single thread order. Asynchronous functions can be executed simultaneously, or at least execute other code while waiting for a response for the asynchronous function.
  2. Callback hell is when you you write code for a complex task that requires several levels and sublevels of nested functions to perform your task and create a callback hell.
  3. We can use promises to deal with callback hell
1 Like

Synchronous functions are run in order and can hold up or block the upcoming functions until the first function has been executed.
While Asynchronous functions have the ability to be paused, and allow for other functions to be ran in the back ground, before the initial function is completed.

Call back hell is when complex operations produce multiple levels and sub-levels of nested functions.

In jQuery, Promises are solutions to callback hell that solves errors.
In Node.js we use extended runtime, producing asynchronous code that appears to run synchronously.

1 Like

What is the difference between synchronous and asynchronous functions?

  • The browser won’t respond to any events whatsoever while it completes the first handler, tasks like these that occupy JavaScript’s full attention are called blocking or synchronous.

  • Synchronous functions are generally not a big problem because we have fast computers, and most even handlers are executed instantly.

  • Whereas a synchronous task will occupy JavaScript continuously until its completion, an asynchronous task can be initiated and then put aside until a later date while our valet gets started on the next takes on its to-do list.

  • You can think of it as JavaScript asking the post office to call it back when the reply arrives, at which point JS can stroll back down there and get the mail.

  • In JavaScript the to-do list is called the event loop.

  • The JavaScript ending actually sports an additional background thread which takes care of managing the event loop.

  • Still, your own code will always run in a single main thread, from a perspective of a programmer, it’s safe to say that JS is single-threaded.

  • In the browser, asynchronous code usually takes the form of Ajax, which is most commonly used through a jQuery wrapper.

What is callback hell?

  • Even a relatively simple operation requires three levels of nested functions, so you can imagine that more complex operations tend to produce even more levels and sub-levels, which is what is poetically known as callback hell.

Which technique can help us solve callback hell?

  • In the browser, a common pattern to deal with excessive callbacks is to use promises.

  • jQuery ships with a simple built-in promise library that enables us to chain callbacks and deal with errors.

  • Which Node.js, we can use a different approach by extending the runtime, we can abstract away the synchronicity and write code that looks synchronous.

1 Like

Synchronous functions execute each command sequentially. The program waits for the current instruction to be completed before moving to the next.

In contrast, an asynchronous function does not wait for certain types of instructions to fully complete. Instead an asynchronous function will execute the next instruction if the current instruction is waiting for an event to happen. Once the event occurs, the computer comes back and completes the instruction that was waiting.

Returning from a later function back to a function that was waiting for an event is known as callback. When many layers of callback occur i.e. callback within a callback, this is referred to as callback hell.

There are different ways of avoiding callback hell. Jquery achieves this through the use of promises where a promise library keeps track of the callbacks.

Other runtime environments such as Node.js use extended runtime so that callback functions have a chance to catch up.

1 Like

What is the difference between synchronous and asynchronous functions?

  • synchronous function is a single-threaded task, java is waiting until function is finished
  • asynchronous funcions can do the job by itself while java script can go to the next job todo (event-loop) function.

What is callback hell?

  • a relatively simple operation with multiple levels of nested functions which all get information back at different times is what is known as callback hell

Which technique can help us solve callback hell?

  • to deal with excessive callbacks is to use promises, this is a simple built-in promise library that enables us to chain callbacks and deal with errors.
1 Like

What is the difference between synchronous and asynchronous functions?

synchronous code is executed in sequence – each statement waits for the previous statement to finish before executing.

Asynchronous operation can be executed without waiting for the previous statement to finish before executing.

What is callback hell?

Callback hell arise when the code have an excessive number of callbacks nested in each other this makes the code too complex to manage.

Which technique can help us solve callback hell?

Using promises.

1 Like

1.What is the difference between synchronous and asynchronous functions?

-Synchronous operations block instructions until the task is completed, while asynchronous operations can execute without blocking other operations. Asynchronous operations are generally completed by firing an event or by calling a provided callback function.

2.What is callback hell?

-Callback hell is any code where the use of function callbacks in async code becomes obscure or difficult to follow. Generally, when there is more than one level of indirection, code using callbacks can become harder to follow, harder to refactor, and harder to test.

3.Which technique can help us solve callback hell?

-Promises are the ideal choice for handling asynchronous operations in the simplest manner. They can handle multiple asynchronous operations easily and provide better error handling.

1 Like

1. What is the difference between synchronous and asynchronous functions?

Synchronous functions mean you do one thing at a time and if that one thing takes a long time to finish then you have to wait for it to finish first before moving on to the next thing. With asynchronous functions you can do new/different things while waiting for the old things to finish, avoiding all waiting around and thus using your time more efficiently.

What is callback hell?

When functions contain a lot of callbacks, and callbacks within callbacks, it makes the code too complicated to follow.

Which technique can help us solve the callback hell?

Using promises solves the callback hell problem.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    Synchronous function the call and not proceed to the next chunk of code until the data is received from that event handler function. In JS this is considered single threaded.
    Single Threaded as in taking care of one function at a time.
    Asynchronous Functions may require the event handler to execute processes that take more time. For example querying data from a third party server or API calls. To wait on such functions to finish would cause the browser to lock up.
    Asynchronous functions avoid this by initiating the function, putting it aside and getting started on the next task. This is achieved by the creation of an event loop.

  2. What is callback hell?
    Callback hell is a term used when a more complex operations produce more levels and sub levels of functions in the code. This can produce some messy code that results in that undesirable Christmas tree effect.

  3. Which technique can help us solve callback hell?
    One solution for dealing with callback hell is to use promises. jQuery has a built in promise library. Node.js takes the approach of extending runtime.

1 Like
  1. Synchronous function executes tasks one at a time in a linear order. Asynchronous function executes tasks in parallel (more than one at a time), so that it can executing on one function while still waiting for another function to get respond from server/ client input.
  2. A callback is a situation when we have a function to be an argument of other function, A callback hell is when we have a lot of callback nested one after an other one, this results a lot of waiting time for the code to be executed.
  3. Promise.
1 Like

Synchronicity is something important to consider when using JavaScript. JavaScript is single-threaded which means only one thread, or action, is able to run at a time. One action has to finish before another action can be started. A lot of javascript actions, or event handlers like click(), take place quite fast so running synchronous actions one right after the other in synchronous fashion is typical, unless one has to communicate with a server. Synchronous functions are sometimes called blocking functions because they will block any other event handler from doing their job until the first action has been completed. Asynchronous functions that start now can be finished later and that allows other event handlers to be initiated while waiting for completion. This has the effect of freeing up the browser so that communication with another server won’t cause delay for the rest of the page.

A callback is a function that is passed as an argument to another function.

Callback hell is where I don’t want to be because it’s so confusing! Actually, this scenario occurs when a cascade of event handlers is in play. Troubleshooting can be tricky when several functions are nested waiting for a server response or other action. The output can be unexpected if certain callbacks finish at different times.

We can avoid callback hell, which is also known as the “Pyramid of Doom”, by using Javascript promises. A promise is an object that exists in one of three states, either pending, fulfilled or rejected. Promises are designed to let us deal with a function’s return of values or error messages by the included methods of then(), catch(), and finally().

1 Like
  1. What is the difference between synchronous and asynchronous functions?

Because javascript is single threaded only one event can happen at a time. Because of this it is important to consider synchronous vs asynchronous. Synchronous functions will occupy the browser until the function is complete, whereas an asynchronous function will put the pending operation aside until it is ready and move on to the next operation in the meantime.

  1. What is callback hell?
    A callback is a function that is passed as an argument to another function.
    callback hell is when more complex operations require a large amount of nested functions, thus lots if event handlers are at play and troubleshooting becomes more challenging and outcomes can be unexpected until it is corrected.

  2. Which technique can help us solve callback hell?
    We can avoid callback hell by using javascript promises.
    Jquery has a built in library of promises that can be used. on the node side in node.js you would use the approach of extending runtime to solve callback hell

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    In a single threaded structure with synchronous function, one tasks can block another task. The second task will not run before the first task is done.
    Asynchronous functions will be initiated and put aside unitl later, so the other tasks can be done.

  2. What is callback hell?
    It happens when you use a lot of asynchronous functions that make callbacks. It will be difficult to control.

  3. Which technique can help us solve callback hell?
    Promises can be used to deal with excessive callbacks. Or by using the fiber package in meteor its possible to write asynchronous code in a synchronous way

1 Like