Asynchronous Programming - Reading Assignment

Answers

  1. Synchronous functions are executed sequentially, in a single threaded environment. Instead, asynchronous functions can be initiated and put aside. These are very useful for API calls.

  2. When there a lot of nested asynchronous functions, we could find in a callback hell!

  3. We can use promises to chain callbacks and deal with errors.

2 Likes
  1. A synchonous function will activate contuously untill it is coplited. A asynchonous function can be put aside while the next one activates.

  2. Callback hell is when there is a lot of complex callbacks.

  3. To solve callback hell you can extract the asynchonous code and make it look synhonous.

1 Like

Answer:

  1. Synchronous functions occupy event handlers until its completion. Asynchronous functions can be initiated and set aside for later, while the event handler goes to the next task.
  2. Callback hell is an abstract problem, which creates the problem of readability of the code. More complex functions require more levels of code, and each level could require more callback functions. It creates unnecessary complexity with a clutter of all callback functions, whose sole purpose is to call back when the request is ready.
  3. We can use “promises,” a technique that helps us deal with callbacks. When you run a function, it tries to return a value. However, it takes time. Instead, you receive a promise of future value or failure. It frees up even handler to move to the next step, instead of getting stuck and waiting for a return.
2 Likes
  1. The difference between synchronous and asynchronous is the manner of execution. In javascript, the execution of code goes from top to bottom in sequential order. Each item gets executed and continues to the next but not until the previous item has finished. However, you are able to run an asynchronous piece of code without the stack getting stuck. For example, an “onclick” event on a button tag would be considered asynchronous as it does not prevent execution of other code.
  2. When a function simply accepts another function as an argument, this contained function is known as a callback function. Callback functions can be named or anonymous functions. The callback function is not run unless called by its containing function, it is called back. Multiple functions can be created independently and used as callback functions. These create multi-level functions. When this function tree created becomes too large, the code becomes incomprehensible sometimes and is not easily refactored. This is known as callback hell.
  3. Callback hell is caused by poor coding practices. Luckily writing better code isn’t that hard. You only need to follow three rules:
    A. Keep your code shallow
    B. Modularize
    C. Handle every single error
2 Likes
  1. What is the difference between synchronous and asynchronous functions?
  • Javascript executes code in sequence. So when you have functions waiting on input from other functions and this is synchronous code; you are trying to do multiple things at the same time. With asynchronous code will go do something else while it waits for the input to execute the awaiting function. It goes to do something else until it is called back.
  1. What is callback hell?
  • When you have a lot of nested functions and a lot of asynchronicity, you can be waiting on a lot of functions to get the needed input and call you back to execute the next functions. Waiting on a lot of callback means a lot of call backs can arrive at once.
  1. Which technique can help us solve callback hell?
  • Chaining callbacks with a library like promises, or by simulating synchronous writing like by using the fibers package in meteor.
2 Likes
  1. Synchronous functions must be executed one at a time, which means they block the program from running other functions simultaneously. Asynchronous functions can be executed at the same time as one another.
  2. Nested event handlers allow our code to become asynchronous, but can leave our code open to becoming overly complex and difficult to read.
  3. Promises.
2 Likes

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

Synchronous and asynchronous operations is about execution order a new task in relation to the current task. Two tasks on a table: current task and a new task.

Synchronous (blocking) - implies that tasks will be executed one by one. A next task is started only after its previous task is finished. Task 2 is not started until Task 1 is finished.


Asynchronous (non-blocking) – implies that task returns control immediately with a promise to execute a code and notify about result later(e.g. callback, feature). Task 2 is executed even if Task 1 is not finished.

2. What is callback hell?
The problem with synchronous functions is that one function can block the executions of other functions. So we will have to use asynchronous functions, but then we need to keep track which of the function has not finshed its tasks yet. In JavaSscript this to-do list is called the event loop. The JavaScript engine actually sports an additional background thread which takes care of managing the event loop. But we will have to pay a price for this “keeping track of the the to-do list”, because asynchronous code requires more complexity. Even simple tasks need sometimes several levels of nested functions. More 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, 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. The approach in the fibers package is simulated synchronicity. It just lets programmers write code in a synchronous style with synchronous control flow, although it doesn’t really make asynchronous code synchronous.

Another aprroach to mitigate the callback hell is using generators. Generators are lightweight co-routines for JavaScript. They allow a function to be suspended and resumed via the yield keyword. Generator functions have a special syntax: function ()* . With this superpower, we can also suspend and resume asynchronous operations using constructs such as promises or “thunks” leading to “synchronous-looking” asynchronous code. A “thunk” is a function that returns a callback as opposed to calling it_._ The callback has the same signature as your typical nodeback function (i.e. error is the first argument).

1 Like
  1. Synchronous function means we have one function executed at a time and an asynchronous function puts on hold a function until it gets an answer and in the meantime executes the next from the wait list.
  2. We have what is called “Callback Hell” when operations tend to have multiple levels and sub-levels of nested functions.
  3. To deal with excessive callbacks we can use promises.
1 Like
  1. A synchronous function blocks the execution of any other code while it is running. Asynchronous functions on the other hand, after being launched can remain in a waiting pattern until their event is triggered without blocking the execution of other code.
  2. Callback hell is heavily nested asynchronous code that tends to become overly complex to manage.

a) Promises are objects that act as proxy for a result that is initially unknown, usually because the computation of its value is not yet complete, or it’s request for additional information has not been answered.
b) Generators are lightweight co-routines for JavaScript. They allow a function to be suspended and resumed via the yield keyword.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    Synchronous functions refer to JavaScript’s single-threaded environment, where only one section of code will run at one time.

Asynchronous functions refer to when have to query third-party sources and wait for callback, which means that we can run synchronous functions in the meantime.

  1. What is callback hell?
    More complex operations tend to produce even more levels and sub-levels in our functions, and which can get messy.

  2. Which technique can help us solve callback hell?
    By taking an asynchronous function ( setTimeout ) and building a synchronous equivalent ( wait ), i.e., using promises.

1 Like
  1. What is the difference between synchronous and asynchronous functions?
    A synchronous function is executed and needs to be finished before the next function can be executed. In case of an asynchronous function, this function can be started and then other functions can be executed until we get a response to return back to the execution of the initial function.

  2. What is callback hell?
    Basically this are multiple levels of nested functions to perform relatively simple tasks. The code gets harder to create, read, maintain and debug.

  3. Which technique can help us solve callback hell?
    Promises can be used to resolve call back hell. They allow to chain call backs and better deal with errors. This does improve readability of the code.

1 Like

Synchronous transmissions are synchronized by an external clock, Asynchronous transmissions are synchronized by special signals along the transmission medium.

Asynchronous codes that was not written in a transparent way, easy to loose in the code when reading. Poor coding practice.

Keep the code shallow, do not nest functions, modularize, handle errors.

1 Like
  1. synchronous functions have to finish before the system can go to the next task
  2. callback hell is when a lot of stuff needs to happen before the system can continue
  3. we can use promises, asynchronous, simulated synchronous, writing good code
1 Like
  1. Synchronous functions block the program’s further execution until something is done. Asynchronous functions do not block the program and wait while the program thread does other task.

2.Callback hell is a situation where several functions are being performed in nested form.

3.We can use promises to solve the problem of call back hell. Promises help us to deal with chained synchronous functions and allows us to also handle errors. Modern javascript libraries and frameworks like Angular and jQuery come with a promise library. Another way of dealing with callback hell in Node.js is to extend the runtime and and abstract away the asynchronicity so as to allow the programmer to write asynchronous code in a synchronous style.

1 Like

Synchronous functions are executed in sequence - one has to finish before the next is executed.
Asynchronous functions can begin a task and then the task can wait for something to happen while the program continues execution, before the task has to be finished.
2. Callback hell occurs when you have a series of asynchronous functions waiting in queue for nested callbacks.
3. Promises can help mitigate the problem of callback hell on the browser side.

1 Like

The article link did not work, so found my answers on google

What is the difference between synchronous and asynchronous functions?

In Synchronous transmission, Data is sent in form of blocks or frames.
In asynchronous transmission, Data is sent in form of byte or character.

Synchronous transmission is fast.
Asynchronous transmission is slow.

Synchronous transmission needs precisely synchronized clocks for the information of new bytes.

Asynchronous transmission have no need of synchronized clocks as parity bit is used in this transmission for information of new bytes.

In Synchronous transmission, There is no gap present between data.
In asynchronous transmission, There is present gap between data.

What is callback hell?

Callback Hell , also known as Pyramid of Doom, is an anti-pattern seen in 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.

Which technique can help us solve callback hell?

Solutions to callback hell

There are four solutions to callback hell:

  1. Write comments
  2. Split functions into smaller functions
  3. Using Promises
  4. Using Async/await
1 Like

Synchronous way It waits for each operation to complete, after that only it executes the next operation.
Asynchronous way It never waits for each operation to complete, rather it executes all operations in the first go.

Callback hell occurs when you have an excessive number of callbacks nested within one another a lot of code ends up looking like a pyramid shape }) at the end.

Keep your code shallow don’t nest functions, splitting code into small pieces.

1 Like
  1. Synchronous executes the programme until it is finished - one at the time, while asynchronous allows us to run another piece of code while waiting for a response from the previous one.
  2. User will have problems reading it as the code is too complex where a callback is done on top of a callback and so on and so forth…
  3. Use of promises.
1 Like

What? It didn’t work?
Did you message that in to us at the support?
Could you post the link here and i’ll make sure it’s fixed?

Ivo

https://www.discovermeteor.com/blog/understanding-sync-async-javascript-node/

its in JavaScript under Reading Assignment: Asynchronous Programming

1 Like