Asynchronous Programming - Reading Assignment

  1. What is the difference between synchronous and asynchronous functions?
    Javascript is single threaded which means that only one section of code can be running at a time. A synchronous function blocks the browser browser until it is finished. Asynchronous functions can be put aside until a later date.
  2. What is callback hell?
    Nested functions.
  3. Which technique can help us solve callback hell?
    Promises.
1 Like
  1. What is the difference between synchronous and asynchronous functions?
    unctions are those that are ‘blocking’ that is to say that they occupy the Javascript engine until their execution is finished. Asynchronous function on the other hand do not block the Javascript thread, instead they wait for execution to complete while the thread does other work.

  2. What is callback hell?Nested event handlers allow our code to become asynchronous, but can leave our code open to becoming overly complex and difficult to read.

  3. Which technique can help us solve callback hell?
    The use of promises ,

1 Like
  1. Synchronous functions are executed in sequence when each statement waits for the previous statement to finish before executing. Asynchronous functions do not have to wait – a program can continue to run. This is done to keep a site or app responsive, reducing waiting time for the users.

  2. Callback hell is any code where the use of function callbacks in async code becomes obscure or difficult to follow. Callback hell is caused by a callback in another callback that’s in yet another callback.

  3. Promises are used to solve callback hell.

2 Likes
  1. Synchronous in JavaScript is that the browser can handle one task at a time or in programming parlance, JavaScript is " Single-threaded". Even in Asynchronous functions, a “to-do lists” or also known as" event loop" will still be done synchronously in the background.

  2. Callback hell refers to a series of complicating nested functions from a asynchronous function.

3)To counter callback hell in browser, we use J Query’s built in PROMISE library. But for Servers, Nodes JS use a different approach. We use Meteor’s Fibers Package. It has the ability to extend the run time to “abstract away” the asynchronicity to look like synchronous .

1 Like
  1. Asynchronous programming: something is going on but you don’t want to wait until that thing is done to continue your program, you want to continue while is happening.
    Synchronous programming: something happens and you wait until is completely finished until you move on to the next task.
    Asynchronous programming is very relevant to JavaScript because we often are making requests to servers elsewhere and it can take a couple of seconds to get your data back and you might not want your program to stall and wait for that data to come back. You want to keep going. This is where callbacks come in. Callbacks were used for a long time until ES6 was released where promises were introduced to the language.
    Promises give you a more elegant way to handle asynchronous data.
  2. Callback Hell also known as Pyramid of Doom is an anti-pattern seen in the code of programmers who are not wise in the ways of asynchronous programming. It consists of multiple nested callbacks which makes code hard to read and debug.
  3. A solution to callback hell is Promises. Promises let us chain instead of nest, they are an alternative to callbacks.
    They are called promises because while they’re handling asynchronous operations they can promise to do something when that operation is finished.
1 Like
  1. Synchronous functions flow step-by-step through single threaded programs whereas asynchronous functions allow a sub-thread to wait for a response while the main program continues.
  2. Callback hell is when many asynchronous callbacks are nested within each other resulting in complex and untidy code.
  3. Browsers can use “promises” while node.js can extend runtime to solve callback hell.
1 Like

1. What is the difference between synchronous and asynchronous functions?
Synchronous functions only run one section of the code at a time. Asynchronous functions run more than one section of the code at the same time.

2. What is callback hell?
When asynchronous programs have functions within functions within functions within functions and so on. This makes the code messy and harder to debug.

3. Which technique can help us solve callback hell?
We can use the promise library to avoid callback hell. AngularJS and jQuery come with the promise library.

1 Like

1.a synchronous task will occupy our computer continuously
until its completion, an asynchronous task can be initiated and then put aside until a later date
2. the level of nested functions can get out of control when thing get complex
3. promises or fibers

2 Likes
  1. Synchronous functions work in sequence, where code execution have to be completed one at a time, while asynchronous functions can happen regardless of sequence and do not hinder the progress of other codes while it is being executed.
  2. Callback hell is when there are too many callback functions nested upon one another, which makes the code confusing to read and difficult to manage.

3.Give names to functions, create modules/libraries for codes, use code linters such as by running
$ standard in the code.

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

Synchronous functions are where a first task need to be completed first before the next task can be started. Whereas an asynchronous function is where the first task can be initiated and put as pending whilst a new task is started

  1. What is callback hell?

Where an operation contains multiple levels and sub-levels of nested functions.

  1. Which technique can help us solve callback hell?

In jquery, we can use Promises. In Node.js, we can use Fibers.

1 Like
  1. Synchronious functions perform the task defined by the function until it is finished and nothing else. The next task can only be started, once the first one is completed. Asynchonous functions can run multiple operations simultaneously.
  2. When you have multiple nested functions waiting for each others callbacks (results of functions that take time).
  3. Promieses.
1 Like

What is the difference between synchronous and asynchronous functions?

JavaScript is a single-threaded, it can only do one task at a time. or a synchronous function, it means that the browser won’t respond to any events whatsoever whilst it completes the specific piece of code (event handler).
An asynchronous function however, can be initiated and then put aside for a while and the next task can be started.

What is callback hell?

This is where the code requires multiple levels of nested functions to enable the asynchronous ability. It becomes very complex to manage the flow.

Which technique can help us solve callback hell?

The use of promises which represent the result of an asynchronous operation. A promise can have 3 states:

  • pending - The initial state of a promise.
  • fulfilled - The state of a promise representing a successful operation.
  • rejected - The state of a promise representing a failed operation.

Once a promise is fulfilled or rejected, it can never be changed - immutable.

1 Like
  1. Synchronous tasks will occupy the system continuously until its completion. Asynchronous tasks can be initiated and then put aside until later while the system gets started on the next task.

  2. It’s when there are too many asynchronous functions running at the same time.

  3. Promise libraries make it possible to chain callbacks and deal with errors.

1 Like
  1. Asynchronous functions can execute multiple event handlers by placing in a task order until they all have been executed and synchronous functions execute the whole code in a single block one task at a time by following a sequence of steps.

  2. A callback from hell is when we call a function within a function and on, which then makes the code unreadable and harder to maintain.

  3. Promises help us solve callback hell.

1 Like
  1. The difference between synchronous, and asynchronous is that a synchronous task will occupy continuously until its completion. Or rather, lets say you have two event handlers run on the same click event. synchronous means that the second handler will not run until the first one has been completed which means that the browser in this instance will not respond to any other events whatsoever while it completes the first handler. so in essence it is a single-threaded. often called Blocking or synchronous.
    Now asynchronous means that other tasks can be called and completed while the first one or other ones are in wait to be called back later or at other time. (not single-threaded).

  2. Callback hell is when you have 3 or more nested functions creating more complex operations which tend to produce even more levels and sub-levels.

  3. the technique which can help us solve callback hell is, promises within the jQuery library. It can enable us to chain callbacks and deal with errors.
    also, in Node.js, there is a different approach. By extending the runtime, we can abstract away the asynchrony and write code that looks synchronous. This is what Meteor does behind the scenes using the Fibers package, which simplifies the code.

1 Like

1.Synchronous function can only happen after another, Asynchronous function execute in parallel.
2.callback hell is where difficult ops tend to produce more level.
3.promises

1 Like
  1. Synchronous functions are blocking functions which occupy javascript until a task is completed whereas asynchronous functions can be initiated while javascript does other tasks.

2.Callback hell refers to when you have many callbacks within callbacks making the code overly complex and harder to read.

  1. A common pattern we use to deal with excessive callbacks is to use promises. jQuery comes with a built-in promise library that enables us to chain callbacks and deal with errors.
1 Like
  1. What is the difference between synchronous and asynchronous functions?
    In synchronous calls, the code execution waits for the event before continuing while asynchronous calls do not block the program from the code execution.
  2. What is callback hell?
    When you have lots of callback functions in your code.
  3. 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.
2 Likes
  1. Synchronous functions are generally not a big problem because we have fast computers, and most event handlers are executed instantly. Asynchronous function on the other hand do not block the Javascript thread, instead they wait for execution to complete while the thread does other work.
  2. Callback hell is an undesirable situation whereby several nested callback functions are needed in order to perform relatively simple tasks. This makes our code harder to write, debug and maintain.
  3. The use of promise
1 Like
  1. A synchronous function refers to a function that will require the computer to wait until completing the code at hand before moving on to the next task.
    An asynchronous function will provide the computer with a callback handler, which will execute once the requested data has been successfully retrieved. This allows the computer to complete other tasks while the earlier asynchronous function is put on hold until its required data arrives.

  2. Callback hell is a term that refers to the complex nature and appearance of nested functions that have not been modularized.

  3. There are several ways to address callback hell. First is with prevention. By modularizing our code into different .js files and by abstracting functions and placing the declarations of our functions at the end of our code (or in a .js file), we an prevent ourselves from getting into callback hell in the first place.
    Second is through tools such as: 1) promises, a cleaner way to handle error codes, and 2) Fibers, a sub library of Future which allows us to use synchronous syntax for asynchronous functions in a Node.js setting.

1 Like