Asynchronous Programming - Reading Assignment

  1. The difference between asynchronous and synchronous functions is synchronous functions in the code can only run one set of code at a time and waits for the results where as asynchronous can run more than one section at the same time.

2.call back hell is one you have one argument that has to wait for another argument to finish thefore it can start another.

3.the use of promises enables us to chain callbacks and deal with errors this can be done through node.js

1 Like
  1. What is the difference between synchronous and asynchronous functions?
  • synchronous functions execute all the code in a single block one task at a time by following a sequence of steps
  • asynchronous functions on the other hand can execute multiple event handlers by placing in a task order until all of them have been executed
  1. What is callback hell?
  • callback hell occurs when JS is coded in such a way that the execution of code happens visually from the top down. (too many nested functions) making it very difficult to debug & maintain
  1. Which technique can help us solve callback hell?
  • By using promises which can be found in JQuery’s promise library
1 Like

1. What is the difference between synchronous and asynchronous functions?
an synchronous function needs to complete before it can go to another function on the task list, while an asynchronous function can go to the task list without it having completed.

2. What is callback hell?
complexity of nested functions

3. Which technique can help us solve callback hell?
promises

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    Asynchronous functions do not wait for external sources of data or information to return something. It continues to execute code. Synchronous executes steps of a function one after the other.

  2. What is callback hell?
    Callback hell is the excessive nesting of asynchronous functions

  3. Which technique can help us solve callback hell?
    Promises is a technique which uses chains of synchronous functions to act as nested asynchronous functions

2 Likes
  1. Synchronous task or functions rely on their completion of execution before moving on, an Asynchronous function allows for it to be put aside temporarily until remaining data is available for its completion.

  2. Callback hell is when you have many levels of nested functions within complex operations. This makes the code harder to read and write and also debug.

  3. A common practice to deal with callbacks is promises.

1 Like

Hi @lfsvamaral,

Nice answers :ok_hand:

Just one comment…

Promises actually avoid having to use callbacks. Instead, the chaining is done with .then() and .catch() methods which perform the success and error handling.

1 Like

Hi @matthurd,

… and the actual term itself refers to the dense and confusing code that results from using multiple levels of nested callback functions. This code is difficult to read, debug and develop further: in other words — hell!

Promises actually avoid having to use callbacks. Instead, the chaining is done with .then() and .catch() methods which perform the success and error handling.

1 Like

Hi @Cryptojunkie,

I don’t really understand what you mean by:

So, just to confirm… asynchronous functions enable the browser to initiate operations that take longer to complete (and would block the browser if performed using synchronous functions), and then to continue executing other code while waiting for the asynchronous operation to complete, or for an error message to be thrown if it fails. The asynchronous function achieves this by including a callback function as one of its parameters.

Maybe what you are referring to is that when the asynchronous operation completes (e.g. event triggered, time elapses, data retrieved from network etc.) the callback is then transferred to the event queue (also known as the task or message queue). Then, when the call stack is clear/empty, the event loop pushes the oldest callback from the front of the task queue to the call stack for execution.

2 Likes

HI @CeesQ,

I don’t understand what you mean here. So, just to confirm…

Asynchronous functions enable the browser to initiate operations that take longer to complete (and would block the browser if performed using synchronous functions), and then to continue executing other code while waiting for the asynchronous operation to complete, or for an error message to be thrown if it fails. The asynchronous function achieves this by including a callback function as one of its parameters.

When the asynchronous operation completes (e.g. event triggered, time elapses, data retrieved from network etc.) the callback is then transferred to the event queue (also known as the task or message queue ). Then, when the call stack is clear/empty, the event loop pushes the oldest callback from the front of the task queue to the call stack for execution.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
  • Synchronous will do the job until completion, whilst asynchronous Javes can be doing one job function and an additional background thread keeps track of the event loop kind of acting like an assistant that can allow Javes(event handler functions)to complete another task potentially before finishing the first.
  1. What is callback hell?
    generally speaking when there is more than 50 nested callbacks that can cause a pyramid of doom

  2. Which technique can help us solve callback hell?
    Promises

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

Synchronous functions are executed in sequential order one after another.
The Javascript code is run in a single synchronous thread. But event handlers
and callbacks can be used to make functions to wait for a trigger event, like
response from database, to happen before executing them. These functions can be
thought as being asynchronous.

  1. What is callback hell?

Callback hell happens, when there are several nested functions that are all
waiting for a callback.

  1. Which technique can help us solve callback hell?

The promises library enables the chaining of operations by utilizing the
.then() method. Meteor’s fibers package enables writing code that looks
synchronous.

1 Like
  1. In synchronous code instructions are executed strictly sequentially, and as long as the execution of one instruction is pending none of the instructions that follow can be executed. In asynchronous code, by contrast, there are sets of instructions that are not executed sequentially but only initiated sequentially. So in this latter case, the non-completion of one set of initiated instructions does not prevent subsequent instructions or blocks of instructions from being initiated or executed.
  2. In order to achieve asynchronicity in code design, there must be created sequences or trees of nested function calls. The call of the first function in such a nested sequence or tree represents the initiation,event, and once it has been issued, the program can move on to other tasks and functions. Moreover, the nested functions that represent the nodes of the tree of which this initiating function is the root are referred to as “callbacks” and may depend for their execution not only on the completion of the initiating call but also on the completion of other, prior callbacks. So in cases where the callback tree is extensive, the corresponding pattern of function nestedness will be highly complex and therefore not only difficult to design but also difficult to comprehend for other programmers. It is this tangle of nested callbacks that is referred to as “calllback hell.”
  3. Callback hell is dealt with by using a so-called “promise library.” This library contains commands and qualifiers that enable a programmer to write highly nested, asynchronous code in a fairly straightforward pseudo-synchronous manner.
1 Like
  1. What is the difference between synchronous and asynchronous functions?
    The difference between synchronous and asynchronous functions is the ability to initiate functions and set them aside while completing other functions. Synchronous functions are single threaded and until that piece of code is complete, no other functions will be initiated. Asynchronous functions can be initiated and left in waiting while another function, or functions, are executed. The original function would have a callback function nested within that initiates after the parameters of the callback have been met (sending/receiving requests, reading/writing documents, executing operations on the server, etc).

  2. What is callback hell?
    Callback hell is when you have a complex program with many functions, nested within functions, which in turn have more functions nested within them, which could have functions nested within them, etc, etc. and many of these functions may have callbacks which are waiting for certain conditions to be met. This could get messy, with multiple functions wanting to initialise at the same time.

  3. Which technique can help us solve callback hell?
    In order to solve the issue of callback hell, jQuery has a built in ‘promise’ library that allows to use promises to chain callback and deal with errors. Another method is to use node.js to extend the runtime and write code that looks synchronous but acts asynchronously. Using ‘Fibers’ in the Future sub-library, is another way of writing asynchronous code in a style that has synchronous control flow.

1 Like

Synchronous functions run in order one at a time, there is no multitasking. Asynchronous is where a task is not fully completed before continuing to the following code executions, so it continues while waiting for code completion above.

Callback hell is where you have so many nest functions within each other that code becomes harder to write and maintain while doing simple tasks.

The use of Promises solves this issue by chaining together callbacks so that we may easier deal with errors.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    In a single threaded environment instructions are executed one after the other. Synchronous functions block further tasks of being completed.
    An asynchronous task can be initiated and then put aside until a later date and the next task on the to-do list can get started.

  2. What is callback hell?
    Callback hell is when multiple layers of callback functions are nested together.

  3. Which technique can help us solve callback hell?
    In the browser, a common pattern to deal with excessive callbacks is to use promises.
    But in Node.js, we can use a different approach. By extending the runtime, we can abstract away the asynchronicity and write code that looks sychronous.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    Asynchronous functions can only be run one after another. In the meantime, no other code can be executed, until the current (asynchronous) function has been completed.
    Synchronous functions can be executed and than be put aside, so that other code can be executed an the (synchronous) function completes later.

  2. What is callback hell?
    Several levels of nested functions are called callback hell.

  3. Which technique can help us solve callback hell?
    By using promises.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    Synchronous functions are executed in order, one by one, i.e only 1st task completes, then 2nd task will be triggered; while asynchronous functions is able to trigger 2nd tasks before 1st task completes.

  2. What is callback hell?
    Excessive levels callback functions stack up, which may cause the program busy or slowness.

  3. Which technique can help us solve callback hell?
    In browser Jquery uses promise package, and in node.js, it uses Fibers package.

1 Like
  1. Asynchronous tasks can be put on hold while executing other code. Synchronous functions are executed one at a time and the next one wont be executed before the latter is done.

  2. When there’s alot of different layers of functions imbedded in one task.

  3. For examples with jquerys inbuilt promises

1 Like
  • What is the difference between synchronous and asynchronous functions?
    synchronous functions run together which can overwhelm the machine so to speak and it occupies the code while it waits for an action to happen. Asynchronous allows us to run other parts of the program while an action is being executed/called… for example utilizing a third party site for some data

  • What is callback hell?
    it runs multiple actions all at the same time and having arguments within arguments causing the code to be rather hard to read and navigate

  • Which technique can help us solve callback hell?
    promises, futures, asynchronous functions on node.js, wait, etc.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    A. Synchronous functions have to be performed one at a time and blocks the browser from doing any other functions.

  2. What is callback hell?
    A. Callback hell is the situation where you have nesting of functions that gets to the point it is “HELL”.

  3. Which technique can help us solve callback hell?
    A. A technique that can help solve callback hell is to use promises.

1 Like