Asynchronous Programming - Reading Assignment

  1. What is the difference between synchronous and asynchronous functions?
    Synchronous functions will perform the task continuously. the asynchronous function will perform another task while waiting for the first task to complete

  2. What is callback hell?
    Too many calls waiting to come back due to the various levels of functions.
    might cause performance issues

  3. Which technique can help us solve callback hell?
    Promises to chain callbacks.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    A synchronous task will occupy full attention until its completion. Nothing else can be done until it is done.
    An asynchronous task can be started and then be put on hold while another task is done.
  2. What is callback hell?
    More complex functions with nested functions tend to produce more levels and sublevels which is called callback hell.
  3. Which technique can help us solve callback hell?
    Different applications provide different techniques.
    jQuery has a built-in promise library to chain callbacks and deal with errors.
    Meteor uses the Fibers package that extends the runtime to make code look synchronous.
1 Like
  1. Synchronous functions block the browser until the event is handled. Asynchronous allow the site to perform different tasks while it waits on further information.
  2. Callback hell is a situation in which multiple nested callback functions are required to do tasks. It makes code more difficult to write and debug.
  3. Promises will save our souls from Callback Hell.
1 Like
  1. Synchronous tasks occupy Javascript’s full attention until completion. In contrast a synchronous task can be set aside after starting to be returned to later when called upon.
  2. Complex programs have multiple levels and sublevels of nested functions all interacting, referred to as callback hell.
  3. In node.js you can use the fibers package; quantifying the asynchronous data and restructuring it to appear more synchronous.
1 Like

1)The difference between them is that synchronous functions block the program’s further task until they are fully executed while asynchronous function do not block program’s further task and they allow to do it
2)Callback hell is a situation where there are several callback nested functions
3)Promise is used to solve callback hell

1 Like

1. What is the difference between synchronous and asynchronous functions?
Synchronous functions perform tasks sequentially, and cannot move on to the next task until the previous one is completed. Asynchronous functions, however, can initiate a task, and then put it aside for later while continuing to work on other tasks. For this reason, asynchronous code will not lock up the browser no matter how long it takes to complete.

2. What is callback hell?
Complex operations tend to require multiple levels of nested functions. Such code can become difficult for a human programmer to understand, and is thus known as “callback hell”.

3.Which technique can help us solve callback hell?
We can use “promises” to help us solve callback hell. JQuery ships with a simple 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?
    ** Synchronous functions - Only one section of the code in the function can run at any one time.* * Asynchronous functions - More than one section of the code in the function can run at any one time.

  2. What is callback hell?
    Callback hell is when many asynchronous functions are held up by waiting for other nested functions to finish execution, essentially holding up the whole process.

  3. Which technique can help us solve callback hell?
    With Promises and allowing our code to wait for others to finish executing so they have the appropriate resources to continue.

1 Like
  1. syncs are blocking the browser, async functions waiting in the background to execute at a later time
  2. a lot of nested functions
  3. using promises from JQuery, written sync style in a async function(or so)
1 Like
  1. synchronous tasks are being continuously computed, and asynchronous tasks can be initiated and “placed aside” until later
  2. Simple operations requires several few levels of nested functions, so more complex operations tend to produce even more “sub-levels” of nested functions which is known as “callback hell”
  3. In browser you can use a jQuery library “promises” that enables to chain callbacks and deal with errors.
    In node.js we can extend the runtime so we can write code that “looks” synchronous.
    Another way is using “Fibers” that enables to write code in a synchronous style with synchronous control flow
1 Like
  1. Synchronous functions do not return (finish) until the code has fully executed. Meaning that, it blocks other functions or tasks from executing until the job is done. On the other hand, asynchronous functions executes while other functions or tasks are executing. They are running in the background.

  2. Callback hell is the situation when callbacks are nested too deep. Screen Shot 2021-04-02 at 14.20.50

  3. A way to deal with the callback hell issue is it use promises. Libraries such as jQuery has a feature to chain callbacks.

1 Like

1. What is the difference between synchronous and asynchronous functions?
Synchronous functions in JavaScript execute each statement of code one by one in order from the top of the program to the bottom. Each statement must wait for the earlier statement to be executed before it can move onto the next. Hence the name synchronous, as they flow in sync one after the other.

Asynchronous functions also are executed from the top of the page to the bottom, usually in combination with synchronous functions, however, instead of causing the program to stop and wait for the asynchronous function to be completed before moving on to the next, it can begin the function, and continue on as the next function is solved at the same time. In other words, asynchronous functions allow for the program to wait to execute them in the background as it continues to work down the page of code and execute the following functions without requiring that the function is complete before moving on. This allows us to have multiple functions in operation in different orders and times depending on the functions requirements of execution.

2. What is callback hell?
Call back hell is when you have one callback inside another, inside another, inside another, and so on. This gets messy quickly and can lead to making issues within the program, and it may also be harder for other programmers to read. This used to be the way things were done until we started using promises.

3. Which technique can help us solve callback hell?
The technique we use to solve callback hell is utilizing promises. Promises allow for asynchronous functions to contain producing code and consuming code. The producing code creates a promise that must be fulfilled, and then it activates the consuming code when completed. This allows for codes to be executed if the previous code has been fulfilled, rejected, or still pending. The way in which you can structure promises is similar to a chain. This makes the code much cleaner than it would be in a “callback hell’’ situation. Promises one of the best ways to write several asynchronous functions in order, in a clean readable way.

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

An synchronous function is one that when called stops anything else from being run until that function has been completed.
An asynchronous task can put the task aside and do other things while the first task is being completed.

  1. What is callback hell?

A term used to describe a functions with many functions within them.

  1. Which technique can help us solve callback hell?

You can use promises that enabling chaining callbacks together and dealing with errors. jQuery has a built in promise library.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    A. In a synchronous programming model, things happen one at a time. When you
    call a function that performs a long-running action, it returns only when the
    action has finished and it can return the result. An asynchronous model allows multiple things to happen at the same time.
    When you start an action, your program continues to run.

  2. What is callback hell?
    A. This is a big issue caused by coding with complex nested callbacks. Here, 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, making it difficult to read and maintain. Also, if there is an error in one function, then all other functions get affected.

  3. Which technique can help us solve callback hell?
    A. Promise

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    synchronous functions will occupy the browser’s attention continuously until its completion whereas asynchronous functions (async and await keywords) can be initiated and then put aside until a later date while our browser gets started on the next task on the code.

  2. What is callback hell?
    When the code has many levels of nested functions and makes it complicated to make sense of. Usually “complex operations tend to produce even more levels and sub-levels, which is what is poetically known as callback hell.”

  3. Which technique can help us solve callback hell?
    In the browser we can use promises. JQuery comes with a built-in promise library that allows us to chain callbacks.
    In Node.js we can use the Fibers (with the Future sub-library) package in Meteor to make the code look simpler by taking an asynchronous function (setTimeout) and build a synchronous equivalent (wait).

Also by:

  1. Writing comments.
  2. Splitting functions into smaller functions.
1 Like

Read the article attached to the right about promises.

Think about the following questions while reading:

  1. What is the difference between synchronous and asynchronous functions? | Synchronous functions are functions that JavaScript focuses it’s full attention on, while Asynchronous functions will be initiated and then put aside until a later time.

  2. What is callback hell? | Callback hell is what happens when a complex program builds levels, sub-levels, and even more sub-levels.

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

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    Synchronous functions only run one section of code at a time therefore blocking further execution until the execution is finished. Asynchonous functions do not block other sections of code from being executed.

  2. What is callback hell?
    Callback hell is dealing with the layers of nested functions within a complex block of code.

  3. Which technique can help us solve callback hell?
    jQuery comes with a built in promise. libraray that chains together callbacks and dealing with errors. If you’re using node.js, you can extend the run-time.

1 Like

1. 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 code doesn’t have to wait. Your program can continue to run. Communication happens when information can be exchanged independent of time.You do this to keep your site or app responsive, reducing waiting time for the user.

Synchronous :
console.log('1')

console.log('2')

console.log('3')
// 1
// 2
// 3

Asynchronous :
Your application does not hang waiting for the two seconds to finish.
Instead it keeps executing the rest of the code and when the 
timeout is finished it returns to afterTwoSeconds.

console.log('1');

setTimeout(function afterTwoSeconds() {
  console.log('2');
}, 2000);

console.log('3');

// 1
// 3
// 2

2. What is callback hell?
You can save a reference of a function in a variable when using JavaScript. Then you can use them as arguments of another function to execute later. This is our “callback”. Callback hell is an anti-pattern seen in code of asynchronous programming. It is used to describe and unwieldy number of nested “if” statements or functions. This code is hard to understand and maintain, and it isn’t scalable.

3. Which technique can help us solve callback hell?

  • JavaScript provides an easy way of escaping from a callback hell. This is done by event queue and promises.

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

  • Promises use .then() method to call async callbacks. We can chain as many callbacks as we want and the order is also strictly maintained.

  • Promises use .fetch() method to fetch an object from the network. It also uses .catch() method to catch any exception when any block fails.

  • So these promises are put in event queue so that they don’t block subsequent JS code. Also once the results are returned, the event queue finishes its operations.

  • There are also other helpful keywords and methods like async, wait, setTimeout() to simplify and make better use of callbacks.

1 Like
  1. Synchronous functions can only complete one at a time in a first in first out order.
    An asynchronous function can let other functions run until it reaches a state where it can be completed.

  2. Callback hell occurs when several functions are nested within a function. This causes the initial function to wait for another function to complete that is also waiting for another function to complete before it can complete. Programmed this way can lead to endless waiting for functions to complete before the initial function can complete and can make troubleshooting difficult.

  3. The Promise technique can be used to solve callback hell, this can chain the callbacks so that they operate in a faster method. The runtime can also be extended so that the code runs in a more sequential order.

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

Tasks that are single threaded or tasks that have to be completed prior to running the next tasks are considered to be synchronous programming. However, asynchronous coding can place a piece of code on hold while it waits for a reply prior to finishing its execution. In parallel with the waiting for a reply or some event the remainder of the program can be executed.

  1. What is callback hell?

When the program has multiple nested functions.

  1. Which technique can help us solve callback hell?

Promise is used to solve the call back hell situation.

2 Likes
  1. Synchronous functions can only run 1 at a time, meaning a secondary function cannot be performed until the previous function is completed. Asynchronous functions on the other hand allow for multiple tasks to be completed.
  2. Callback hell is when there are several nested callback functions. This can result in unnecessarily complicated code which can be difficult to maintain and/or trouble shoot.
  3. We can use promises in jQuery or Fibres in Meteor which uses asynchronous code to avoid callback hell.
1 Like