Asynchronous Programming - Reading Assignment

  1. Synchronous functions have to run in order, completing the 1st task before another task starts, where as Asynchronous functions are able to be initiated and then executed at a later time.

  2. Callback hell is the addition of new levels and sub-levels to complex operations.

  3. Promises is a technique that can help us solve callback hell.

1 Like
  1. Synchronous functions - the execution of functions is one after the other, so one function must finish until the next function starts, Asynchronous functions - its a bit like multitasking functions, where a function will start and even if it needs more time to complete, another function will already start
  2. When we use a lot of asynchronous functions in our programming this callback hell can occur. It means that this complex operations produce a lot of levels and sublevels and this creates problem when functions must interact and call back to each other.
  3. In the browser, we can use the so called promises. jQuery ships with a simple built-in promise library that enables us to chain callbacks and deal with errors. In node.js, we can extend the runtime and this way we can abstract away the asynchronicity.
1 Like

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

A synchronous function is where JavaScript executes code continuously until its completion. Whereas, an asynchronous function can be initiated and put aside for future use while JavaScript executes the next code in its order.

2. What is callback hell?

Callback hell is when there are several levels of nested functions in one or more operations, significantly adding to their complexity. Excessive callbacks become callback hell.

3. Which technique can help us solve callback hell?

Using promises. jQuery has a built-in promise library that enables chain callbacks.

1 Like

What is the difference between synchronous and asynchronous functions?

A synchronous function will consume a job until it is completed,

but an asynchronous function can be initiated and set aside for later

completion.

What is callback hell?

Callback hell happens when there are too many callbacks nested within one

another, making the code difficult to maintain.

Which technique can help us solve callback hell?

We can utilise promises to assist us get over callback nightmare.

JQuery includes a basic built-in promise library that allows us to sequence

callbacks and handle faults.

1 Like

1)Synchronous functions or block functions are functions that lock the browser while executing code. Asynchronous functions use writing files executing operations on the browser, APIs. Using nested functions(for call back arguments) like node.js, JavaScript on the browser.

  1. Callback hell: The nested functions from asynchronous functions create a complex operation through other levels and sub-levels of nested functions creating what is referred to as callback hell. One function waiting for the next, the next, the next…callback.

  2. The best way to deal with callback hell is to use “promises” , a jQuery library that enables us to chain callbacks and deal with errors. With Node.js we can use a different approach abstracting away a synchronicity and write code that looks synchronous. Meteor does a synchronous code synchronous. it let us write code in a synchronous style with synchronous control flow.

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

Once invoked, a synchronous function runs until it is completed. With regard to synchronous code in general, each line of the program code is executed one after the other according to the control flow, i.e. usually from top to bottom. In a situation where a task needs to receive a response from a third-party server (or any action that consumes a noteworthy amount of time and causes a delay in the execution of the code), synchronous code blocks up the whole program because no other code of the program will run until the current task is completed. This means that synchronous code can be really inefficient with regard to time management because if there is a lot of such idle time, it is basically a large amount of wasted time.

This is where asynchronous code comes into play. Asynchronous code allows to initiate a task and, if there is a situation when a piece of code is waiting for a response to continue operations, the task is put aside and the next part of the program is executed. Once the program is informed that it has received the response necessary to complete the idle task (callback), it goes back to this task and finishes it. Altogether, asynchronous code avoids blocking up time spent on “waiting” and uses time much more efficiently because the execution of a task may be put on hold and resumed at a later stage when all necessary input is available.

  1. What is callback hell?

Put simple, a callback in JavaScript is a nested function that is triggered by a specific event (an event could be that the necessary data are now available). A task may include many subtasks, each with a callback. This can create a messy piece of code, which is hard to read, prone to error and no fun to debug.

  1. Which technique can help us solve callback hell?

Promises were introduced as the modern style of asynchronous code. Promises come in a jQuery library and are specifically designed for handling asynchronous operations. Using promises, it is possible to chain pieces of asynchronous code (callbacks) together, which improves both writing and reading the code. Furthermore, promises also improve error handling because a particular block at the end of a chain of callbacks handles all the errors. With traditional callbacks, error handling must be done on the individual level of each callback.

Another possibility comes with Node.js, which simulates synchronicity, meaning that the programmer can write asynchronous code in synchronous style.

1 Like

1)When calling a synchronous function, it must complete it’s task before any code external to the synchronous function can begin to execute. When calling a asynchronous function, the program can continue to concurrently execute code external to the function while the asynchronous function continues to run until it completes it’s task.

2)Any function that is passed as an argument of another function is a callback function. Callback hell refers to code consisting of multiple nested callbacks which makes the program difficult to read and debug.

3)Promises and Meteor.

1 Like
  1. The difference is that synchronous code is executed when called whereas asynchronous code executes at some other time either after a different function runs or maybe after some condition is met.
  2. Callback hell is when a piece of code triggers another sublevel piece of code which then triggers another piece of code adding to the computational complexity of the program. If this gets too far out of control the program becomes unstable and runs very slowly or does not run at all. This also makes it much more difficult to debug as the levels of complexity become too confusing to find the mistake.
  3. The technique we should use to solve callback hell is called promises. Promises in jQuery allow us to chain callbacks and streamline our code.
1 Like

1. What is the difference between synchronous and asynchronous functions?
synchronous only runs one section of a code function at a time.
Asynchronous allows for others sections of the code to run while it is waiting for the initial task to complete. It allows for multi-tasking.

2. What is callback hell?
callback hell is when there are many asynchronous functions nested within a function. It becomes extremely complex and difficult to read, thus increasing the chance of errors.

3. Which technique can help us solve callback hell?
Promises can be used to deal with excessive callbacks.
In JQuery there is a simple built-in promise library that enables us to chain callbacks and deal with errors.
Node JS can can abstract away the asynchronicity and write code that looks synchronous by extending the time runtime.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    Synchronous functions can only run one piece of code at a time whether asynchronous one can do multiple tasks at the same time as explained in the example of the article.
  2. What is callback hell?
    When you have too many asynchronous functions nested within a function so it gets extremely difficult.
  3. Which technique can help us solve callback hell?
    Promised can be used to solve the problem, in JQuery there is a simple built-in promise library.
1 Like

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

Tasks that occupies full attention are called blocking or synchronous . Whereas a s ynchronous task will occupy attention until its completion (takes time), an asynchronous task can be initiated and then put aside until it is completed. There is no delay for executing next task. It will be still executed when done, but in that time next task can continue.

2.What is callback hell?

It is an operation that requires, for example, more levels of nested function. More complex operation tend to produce more levels and sub-levels.

3.Which technique can help us solve callback hell?

With using promises technique (jQuery). It is technique in the browser that deal with excessive callbacks. It has simple built-in promise library that enables us to chain callbacks and deal with errors.
In node.js we can use approach which extend the runtime and write the code that look synchronous.

1 Like
  1. Synchronous functions can only be executed in order, while asynchronous functions can run in any order.
  2. Callback hell is the term used when there’s a huge number of embedded functions in asynchronous programming.
  3. In browsers, we can use promises, which “enable us to chain callbacks and deal with errors”. In server programming, we can also write asynchronous programs, that are very similar to synchronous.
1 Like
  1. What is the difference between synchronous and asynchronous functions?

A synchronous function will occupy the event handler until the task is complete while an asynchronous function, although initiated, be put to the side and will not be completed until the task at hand has been first.

  1. What is callback hell?

When there are more and more levels and sub-levels of functions being produced

  1. Which technique can help us solve callback hell?

promise.jQuery deals will excessive callbacks by chaining them and dealing with errors. There’s also node.js, which extends the runtime of a function to make the code look synchronous.

1 Like

where is link of the article ? i couldn’t found it.

Hi @hinazahoor,

You can use the resources given in the course slides (eloquent javascript book, external links etc.).

A.Malik

I have read the concepts from book and also google it. But It looks like course was referring to some article attached. I couldn’t find that article.

But it’s ok. I got the concept. That’s more important.

Thanks for reply.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    Synchronous functions can only run one at a time. One after another. Asynchronous functions can be ran simultaneously with other functions. (Those are typically functions, that are waiting for response from the server etc…) It can be initiated and then put aside until completed.

  2. What is callback hell?
    Callback hell is a situation, when you nest too many functions into single function and you create way too many sub-levels of functions with callbacks.

  3. *Which technique can help us solve callback hell?
    Using Promises library. In Node.Js we can eliminate the issue bybBy extending the runtime, so we can abstract away the asynchronicity and write code that looks sychronous.

1 Like
  1. Synchronous functions don’t return any work until the task has been either completed or failed, whereas asynchronous functions can be initiated and sit idle until ready to return.
  2. A number of nested functions that create levels and sub-levels of complexity.
  3. By using promises or sub-libraries that allow you to write code that looks synchronous.
1 Like

1- Synchronous functions execute the codes in the coding order, but asynchronous functions might skip some lines of codes including callback functions until their requirements are fulfilled.

2- Callback hell is the mess that occured, because of too many nested callback functions.

3- Using Promises can help us to deal with callback hell, and it became easier to both write and read the code.

1 Like

A.1. Synchronous functions in JS mean that every statement of the code is executed in sequence i.e. one by one. A statement has to wait for the previous statement to be executed.

Asynchronous functions in JS mean that particular statements of the code can be executed immediately. This allows faster user interface and further statements of the code are not blocked until the current code is executed.

A.2. Callback hell is when each and every callback takes an argument that is a result of the previous callbacks. In this manner, the code structure looks like a pyramid and makes it difficult to read and maintain.

A.3. Callback hell can be solved by implementing promises.jQuery. This contains a built-in promise library that enables to chain callbacks and deal with errors.

A promise is a returned object from any asynchronous function to which callback methods can be added based on the previous function result.

1 Like