Asynchronous Programming - Reading Assignment

  1. A synchronous task will occupy the computer continuously until its completion, an asynchronous task can be initiated and then put aside until a later date.
  2. A complex operation with several levels and sub-levels of functions.
  3. The simplest way is to use Promises. JQuery has its own library of promises, as does Node.js.
1 Like
  1. Synchronous functions are functions that - while they’re running - do not allow the execution are other codes. They’re as if they’re occupying the execution queue, not allowing to proceed to new tasks until the code the previous code has been fully executed.
    Asynchronous functions allow the execution of a task while also “putting it aside” - if needed - to allow the the calculator to go on with another task - not wasting waiting time - getting back to the suspended code only when it’s retrieved the need data and is ready to proceed again.

  2. It’s code that makes uses of a lot nested functions, each one calling back another function to get a task done. It can easily get messy and produce errors if not careful.

  3. The technique used is called “promises” - but from the article I didn’t really get much, except that it makes use of synchronous syntax while instead still making use of asynchronicity.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    whereas a synchronous task will occupy Javes continuously until its completion, an asynchronous task can be initiated and then put aside until a later date.
  2. What is callback hell?
    more complex operations tend to produce more levels and sub-levels, which is what is
    poetically known as callback hell.
  3. Which technique can help us solve callback hell?
    using promises.
1 Like
  1. Synchronous functions operate one at a time and stop all other executions until they are done. Asynchronous functions execute in concurrence with other functions and don’t need to wait for other functions to finish execution before they can go on ahead with what they’re doing.
  2. a situation where several functions are being performed in nested form.
  3. The use of Promises.
1 Like
  1. Synchronous functions are the ones which require one task to complete before executing the next task. Asynchronous functions can be initiated and then put aside for other tasks to complete.

  2. Callbacks within callbacks within callbacks is called Callback Hell.

  3. A common way to deal with excessive callbacks is promises. Promises enable us to chain callbacks and deal with errors

1 Like

1.an asynchronous function can be initiated and then put aside until a later moment while other
functions get are activated on the next task on to-do list. Synchronous functions do one function at the time.

  1. a function consisting of multiple sub-level functions. Functions calling back to other functions

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

1 Like

1.)

  • Synchronous functions block instructions until the task is completed
  • Asynchronous functions can execute without blocking other operations.

2.)
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.)

  • The use of Promises can solve a callback hell situation.
1 Like

1.Syncronous (or blocking) functions are functions that follow wach other. Only one function can be executesd at a given time. They ‘respect the control flow’, the next function given isn’t exectued before the previous is concluded. We name them event handlers. When the first event handler is completed - function executed, the browser will respond to further events. Because we have fast computers syncronous functions are executed without problems (instantly).

Asyncronous functions can be initiated and then put aside until later. The next event that it should be executed after an asyncronous function is, is keept track of with the event loop. JS has an additional background thread which takes care of managing the event loop.
JS code will however always run in a single main thread.
In the browser, asyncronous code usually takes the form of Ajax (most commonly used through a jQuery wrapper) - e.g. two event handlers: function () {} and $.get() function.
The second handler is known as callback.

2.Callback hell is called becase more complex operations can/tend to produce even more levels and sub-levels. Even relatively simple operation require three levels of nested functions.

3.Promises are used to deal with excessive callbacks:

  • in the browser promises are built-in in the jQuery library and enable us to chain callbacks and deal with errors,
  • in Node.js by extending the runtime , we can abstract away the asyncronicity and write code that looks syncronous (e.g. Meteor does this using the Fibers package).
1 Like
  1. Synchronous functions are executed in a single-threaded event loop in a specific order, whereas asynchronous functions are executed in a separate thread in the background as information is queried etc.

  2. Callback hell is a name given to the potentially complicated series of callbacks in more complex JavaScript operations which can produce many levels and sub-levels of nested functions.

  3. A technique that is used is writing a function that simply executes after waiting a defined amount of time (often expressed in milliseconds).

1 Like
  1. JavaScript is considered to be single-threaded, such that only one task at time can be handled. In this kind of environment only one single section of code at time can be running. In such case we talk about synchronicity. This mean that even making API’s call, a synchronous function would perform the call and waiting until the response is received, locking the execution of rest of the code. With asynchronous function, on the other hand, task can be initiated and then put aside. This allows to compute different actions without locking the code with pending action.
  2. A callback hell happen when a code has a lot of nested functions, when one function callback another function and so on. This type of situation make harder to understand the code an maintain it.
  3. A typical way to solve this problem is using “promises”. JQuery help us to handle this kind of issue enabling to chain callbacks and deal with errors. Another way to handle this problem is using Node.js that extend the runtime an make asynchronicity looks like synchronicity.
1 Like
  1. Synchronous functions only allows for one code to be ran at a time
    Asynchronous allows for more than one section of the code to execute on javascript
  2. A callback hell is when asynchronous functions are running and are being held up waiting for nested functions to finsih their execution.
  3. We can use promises to deal with callback hell. Promises provides assistance to deal with the chained synchronous functions and allows us to also handle errors
1 Like

1 - A synchronous task will occupy Javes continuously until its completion, on the other hand an asynchronous task can be initiated and then put aside until a later date while our valet gets started on the next task on his to-do list.

2 - A relatively simple operation requires three levels of nested functions. So more complex operations tend to produce even more levels and sub-levels, which is what is poetically known as callback hell.

3 - 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.
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?

Javascript is single threaded and can be looked at like a single path for all instructions to follow this is the synchronous nature of always walking forwards never taking a turn or walking down a path that always ends with the same destination. being synchronous means that one function would follow another function once the first function is finished an this cause a block or time to be consumed without any benefit. Asynchronous means that functions can be carried out whilst waiting for the response to the previous functions and once a result is available from an async function it can then be used further within the code.

  1. What is callback hell?

is when there are loads of call backs and each cant be resolved or defined quickly enough this is resolved by promises.

  1. Which technique can help us solve callback hell?

promises.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    Synchronous function run one at a time in a sequential manner, whereas asynchronous functions can manage multiple tasks at the same time, or at least use various methods to overcome the issues seen with synchronous functions.

  2. What is callback hell?
    Callback is the name given to a situation where nested functions are waiting for answers before then can move on to the next task, causing congestion, a bit like a traffic jam inside the programme.

  3. Which technique can help us solve callback hell?
    A technique called promises which is shipped with JQuery enables callbacks to be chained together and errors dealt with to overcome callback hell.
    Another method is to use Node.js to extend the runtime, abstracting away the asychronicity and writing code that looks synchronous.

1 Like
  1. Synchronous functions cannot be executed until the task at hand is finished. Asynchronous functions initiate the task and this can be completed at a later time.
  2. Callback hell is when creating a complex nested callbacks when coding.
  3. Using promises
1 Like
  1. synchronous task = will occupy the JS engine until completed;
    asynchronous task = can be initalised and then put aside to work on another task
  2. callbacks are created when to many functions are nested in functions - it is really hard to read and interpret the code - the outcome of such is a callback hell
  3. e.g. the use of Promises or similar libaries can help to solve the callback hell.
    or better do structure your code as from the beginning. keep it shallow and modularize it. Create reusable functions!
1 Like
  1. The difference between a synchronous and asynchronous function is that synchronous tasks have to complete before other tasks can begin, while asynchronous tasks can be delayed while another task is executed.

  2. Callback hell is when callbacks are located within other callbacks, repeatedly.

  3. Promises can be used to solve this by allowing us to write code which seems to be synchronous but is really asynchronous .

1 Like
  1. synchronous doing one thing at a time and waiting for each to finish before going on to the next. asynchronous start a job and when its finished it will signal an event. you dont know when the event will happen, so you go do other things until the event happens.

  2. callback hell is when you have a chain of things that have to happen one after another but each one will run a call back. you can start them off all at once and wait for the call backs but instead have to have first call back start the second operation that makes another call back and so on. You can get lost in the call backs. some cases you have to create chain of function calls instead of a sequence of calls because each call has a call back.
    { y=a(x);
    z=b(y);
    w=c(z);
    }
    does not work so you have to do
    { a(x, (y) => {
    b(y, (z) => {
    c(z, (w) => {
    // do something with w
    //return all the way back
    }
    }
    }
    the code is confusing logically its got to be done synchronously.

  3. use packages that abstract the Asynchronous parts so we can write the code as if it was synchronous, There is something call promises that is used https://web.dev/promises/

1 Like
  1. synchronous functions must finish what they are doing before moving onto the next function where asynchronous functions can handle executing multiple functions at once.

  2. Callback hell is when you have many functions nested within one another that are all waiting for the functions before them to be executed.

  3. We can use a jQuery promises.

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

We say that Javascript is single-threaded, meaning one section of code can run at any one time and waits for the previous step to execute completely, this is what we call a Synchronomous function. On the other hand an Asynchronomous function can defer operations, a task can be initiated and then put aside until a later date.

  1. What is callback hell?

Simple operations require sometimes many levels of nested functions, the more complex the operations the more level and sublevels are needed which make code hard to read and debug.

  1. Which technique can help us solve callback hell?

In the browser one way is using Promises, which is an object that may produce a single value sometime in the future. jQuery has this in-build library that helps us chain callbacks and deal with errors.

We can also turn to Meteor, as it uses the Fibers package, with Fibers we can use the Future sublibrary, which allows us to write code in a synchronous style, with synchronous flow.

1 Like