Asynchronous Programming - Reading Assignment

1. What is the difference between synchronous and asynchronous functions?
Synchronous functions are those that block the single-thread until the whole function’s code were executed. Whereas a asynchronous functions are executed, the single-thread doesn’t be blocked because the code of this type of function is inside a callback function that will be executed just when the asynchronous function will be done.

2. What is callback hell?
When there are many nested callback functions one inside another.

3. Which technique can help us solve callback hell?
We can solve callback hell by using JavaScript’s class called Promise.

  1. Synchronous functions are those that execute in order of succession; asynchronous functions are those that execute in parallel with other functions.
  2. Callback hell is when there are multiple nested callback functions, particularly for simple tasks, which make it difficult for us to maintain and debug code.
  3. Promises help us deal with callback hell. In Node.js we can extend the runtime and abstract away from the asynchronicity and write async code in a synchronous manner.
  1. Synchronous functions when initiated does not allow execution of any other function and executes one at a time whereas asynchronous functions can be kept on hold and allow other functions to be executed.
  2. Functions within functions which are the nested functions are known as callback hell.
  3. Callback hell problem can be dealt by using promises. jQuery comes with a promise library which takes care of callbacks and errors.

1. What is the difference between synchronous and asynchronous functions?
The synchronous function blocks the browser because it waits for the function to complete and asynchronous function does not block the browser. When the asynchronous function ends it sends a callback back to the browser. While waiting for the callback the program can execute another tasks.
2. What is callback hell?
Callback hell is when multiple functions are executed in a nested way. Another words it is when one callback is nested within another callback which is nested in another callback. The code become very complex and hard to read.
3. Which technique can help us solve callback hell?
We can use promises which represents the result of an asynchronous operation. We can also instead of using anonymous functions use named functions to make the code more readable.

What is the difference between synchronous and asynchronous functions?

synchronus functions do block the programm while they are running. Asynchronus functions can be runn in paralell or at least it looks like it.

What is callback hell?

Complex operations tend to produce even more levels and sub-levels, which is what is poetically known as callback hell.

Which technique can help us solve callback hell?

Jquery uses promises to deal with callbacks
In Node.Js the runntime can be expandet so the code can run synchronus.

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

Synchronous functions must completely be executed before moving on to the next computational process in the event loop. The event loop is what keeps track of the tasks JavaScript has yet to complete in a program, think of it like a to-do list for JavaScript. Once the block of code containing your synchronous function has fully executed, Javascript will move on to the next item on the list.

Asynchronous functions do NOT have to fully complete before moving on to the next item in the program. This allows them to be initiated and then put on hold while the function waits for the output–like the output form a API call–to be returned before moving on with the code associated with that particular operation. While this process of initiation and consequential waiting is going on, JS can put the whole asynchronous event on hold as it completes other functions and computational operations. Then, when the output is received, JavaScript will jump back to finish executing the asynchronous function it put on standby before moving on to finish executing the rest of the program. Essentially, asynchronous code can be thought of as happening in the background.

This setup allows JavaScript to execute multiple operations at once, while simultaneously acting as though it was operating in a single threaded environment–an environment that only takes care of one task at a time.

2. What is callback hell?

Here is an example of some code written in the callback hell format:

trackUser = function(userId) {
users.findOne({userId: userId}, function(err, user) {
var logIn = {userName: user.name, when: new Date};
logIns.insert(logIn, function(err, done) {
console.log(‘wrote log-in!’);
});
});
});

A situation where there are several nested functions that all rely on the callbacks of the parent function in order to run…essentially this means that the program includes an overarching parent function that has child functions which will run based on the events of the parent function first being executed. Although this all runs fine, and is a way to allow the code to execute other tasks asynchronously while waiting for the callback events to execute (and subsequently call their associated functions), it can very quickly get messy and confusing for anyone trying to decipher the code…even the developer who wrote it!! Thus this messy coding style earned the name “callback hell” in honor of the hellish conditions created for all those who try to understand the verbose asynchronous coding style. Luckily, there is a better way!!!

*Side note: Here is a definition of a callback function in Jake terms:

a) Composed of 2 components:

  1. Callback event - a condition that must be met in order for the associated/linked callback function to be summoned. Without this even being executed, the associated callback function will NEVER run.
  2. Callback function - a function–generally an anonymous function–that performs some action based on a certain event taking place. This function will remain dormant in it’s parent function until the associated event fully completes. However, after the callback event’s condition is met, JS will immediately jump to that line to execute the callback function.

b) Callbacks are important in the control flow of a program. They allow a task (callback event) to be initiated and ignored by the JS engine until the task is fully completed. During this ignored period, JS can execute other tasks it has waiting in it’s que acting out code asynchronously (in the sense that while this one event is waiting to be completed, other events can also be completed simultaneously). After the task (callback event) completes, the JS control flow immediately jumps back to the previously ignored callback event to complete the associated callback function that was waiting for the event to finish up in order to execute. After it’s done executing, JS will resume where it left off in executing the rest of the other code in the program.

3. Which technique can help us solve callback hell?

Of course there’s a better way! Promises are one of the keys JavaScript developers use to help them overcome the problems associated with ~callback hell~. So what exactly do they let us do? This simple built-in jQuerry library allows us to chain callbacks and better deal with errors. However, now there is an even better technique available in the Fibers package of Meteor.

Fibers extends the run time so you can abstract away asynchronicity to write code that looks synchronous while still running within an asynchronous environment. You can find this package in the “Future” sub-library. For example, you could change the asynchronous function (setTimeout) to the synchronous equivalent of (wait) using Fibers. This is just simulated synchronicity, so you still will find all the benefits of asynchronous execution (because that’s what’s really happening) without the hassle of having to write in a form that yields callback hell…instead you’ll have the freedom to write with a synchronous control flow in mind.

Below, you’ll find the code that was written above cleaned up and made easily readable by implementing the “Fibers” package below:

trackUser = function(userId) {
var user = Meteor.users.findOne({userId: userId});
logins.insert({userName: user.name, when: new Date});
console.log(‘wrote login!’);
}

Hello everyone ?? I need help with .JS i am having issues

i am not sure what I did… but it was of my making…Please and TY

What is the difference between synchronous and asynchronous functions?
The difference is timing.
For synchronous functions, all code is executed in one single block, following a sequence of steps, given line by line in the code.

For asynchronous functions, some part of the have to wait for information previously requested.

What is callback hell?
Is the problem that results from waiting for information in an asynchronoues function. The problem get worse if you have multiple levels of nested functions.

Which technique can help us solve callback hell?

Callback hells can be deal with using the promeses libary in jQuery.

What is the difference between synchronous and asynchronous functions?
Synchronous functions execute something and let other tasks wait until the original task is completed. Asynchronous functions start the task, don’t wait until the task is completed before starting a new task. In this last version we need the event loop to monitor responses from tasks that finish.
What is callback hell?
If we need callbacks to be built into the functions, complex programs can become very complex because we could need multiple levels of nested functions.
Which technique can help us solve callback hell?
A technique called “promises” can help us with that. It enables to chain callbacks and deal with errors

These answers are my rough interpretation so they will lack formality since I am still a n00b at the language of programming

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

The difference between synchronous and synchronous functions is that synchronous only operates one task at a time time, enabling event handlers to be executed immediately. Meanwhile, asynchronous functions enable to execute numerous event handlers by placing them in a task order until all of them are executed.

2.What is callback hell?

Since Javascript operated in a single-threaded operation. Thus, when working with the backend programming lannguage NodeJS, simple operation function can composed of numerous levels of nested functions. Thus, the complex operation will result in this callback hell.

3.Which technique can help us solve callback hell?

A solution to callback hell will depend on what the programmer is working on. In jQuery, a solution is for the programmer to use a technique called promises. In promises, the simple built-in library allow to chain callbacks and deal with errors. However, in NodeJS, where programmers are working with servers, callback hell can be resolved by extending the runtime of the calllback. In this way, the asynchronous functions is reverted to an operation that works as it was synchronous function.

  1. Synchronous functions complete one task and then move onto the next one. Asynchronous functions allow multiple tasks to be completed at the same time, this can save time.

  2. Callback hell is when there are many nested functions waiting for a response and it is difficult.

  3. Promises can be used to simplify code from callback hell.

1. What is the difference between synchronous and asynchronous functions?
Synchronous functions is a blocking method that have to be first accomplish to continue with another task. Asynchronous functions are the opposite, they can start an execution task and continue with other tasks while the response of the first one arrived. They are non-blocking functions in a single-threading environment
2. What is callback hell?
A callback is the response of a function. When we are working with asynchronous functions, we can fall in too many nested functions which is a source of errors.
3. Which technique can help us solve callback hells?
There are some techniques to deal with callback hells, one of them is modularity and more precisely Promises that enables us to chain callbacks and deal with errors.

  1. Synchronous functions block other operations while it is being executed whereas Asynchronous functions allow for other operation while the previous operation completes and then it comes back to it.
  2. It refers multiple callback function nestings for simple tasks even.
  3. By using promises. It can allow for chaining callbacks and deal with errors.
    And in Node.js we can use a synchronous style of programming where the asynchronicity is abstracted away e.g. by using Meteor.
  1. Synchronous functions are executed in the order they are placed, for the next task to happen, the previous one has to be completed. Asynchronous functions can skip certain parts of the code and continue even of the previous statement has not been completed and even return to it later if necessary.
  2. Bureaucracy at its best. Is when in a code you have several statements nested that call each other in a long succession making the whole code way longer than it should be and really difficult to track due to this messy work-train.
  3. There is something called “Promises” that allow to plan ahead and make the “Callback hell” work in a more or less parallel fashion, so the waiting line won’t be that “hellish”.

–Synchronous functions are executed in sequence with each statement waiting for the previous statement to finish before executing. Asynchronous functions do not have to wait for previous statements to finish.
–Callback Hell is when coders write JavaScript in a way where execution happens visually from top to bottom.
–Callback hell can be handled using promises, splitting code into modules, keeping code type shallow, & handling every error.

  1. The difference between sync and async functions is that an async function is put on hold and is not executed completely at once. JavaScript comes back at a later time to completely execute it. A sync function gets executed once and it is done.

  2. Callback Hell is the expression used when a single operation uses various levels of nested functions.

  3. A technique is called Promises, which enables programmers to chain callbacks and handle errors.

Once started, a synchronous function must execute that function until that function is done. Everything else on the webpage must wait until that function is done. With an asynchronous function, you can start running the function and go on to something else. When the asynchronous function completes, you can be notified through a callback function.

While asynchronous functions may allow your website to do more things, these functions do have disadvantages. If you have to run multiple asynchronous functions in an order, you must nest multiple callbacks in what is often called callback hell.

Promises help you avoid callback hell.

1. What is the difference between synchronous and asynchronous functions?
Synchronous functions are in a single threaded enviroment, running one section of code afer another in a linear fashion. An asynchronous task can be initiated and then put aside until a later date, In the browser, asynchronous code usually takes the form of Ajax. Ajax is most commonly used through a jQuery wrapper:

2. What is callback hell?
As programs become complicated with more nested functions and produce even more levels and sub-levels, this is what is poetically known as callback hell.

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

What is the difference between synchronous and asynchronous functions?

  • Synchronous functions execute tasks in a linear or serial fashion
  • Asynchronous functions execute in “parallel” and can wait for interactive input or execute other functions before continuing

What is callback hell?

  • Where nested asynchronous functions become unwieldly, difficult to debug and maintain.

Which technique can help us solve callback hell?

  • Use jQuery with Promises to handle multiple callbacks
  • What is the difference between synchronous and asynchronous functions?
    Synchronous functions are those that until don t finish their execution the thread can not keep going , they block the thread till the function execution is done. And on the other hand the async functions can keep running in the back meanwhile the the thread keep going.

  • What is callback hell?
    A callback is when you have a function like an argument of other function , so a callback hell is when you have a lot of callback functions nested one after an other one making your code difficult to debug or read.

  • Which technique can help us solve callback hell?
    We can solve callback hell with promises.

1 Like