Git Product home page Git Product logo

callback-hell's Introduction

callback-hell

callback-hell is a repository that demonstrates the problem of callback hell in JavaScript, and provides examples of how to avoid it.

What is callback hell?

Callback hell is a term used to describe the situation when the code that relies heavily on callbacks becomes unreadable and difficult to maintain. This often occurs when dealing with asynchronous operations in JavaScript.

How does this repository help?

This repository provides examples of callback hell in JavaScript and demonstrates how to avoid it by using various techniques such as Promises and async/await. The examples are organized in a way that makes it easy to compare the code that uses callbacks with the code that uses the alternative techniques.

How to use this repository?

This repository can be used as a reference to learn about callback hell and how to avoid it. You can clone this repository and run the examples to see the difference between the code that uses callbacks and the code that uses Promises or async/await.

Contributing

If you find any issues or have suggestions for improvements, feel free to open an issue or a pull request. Contributions are always welcome!

License

This repository is licensed under the MIT license. See the LICENSE file for more information.

callback-hell's People

Contributors

dandv avatar domenic avatar erithmetic avatar evangoer avatar luk- avatar max-mapper avatar preciousimo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

callback-hell's Issues

Can you add some words on how callback arguments get set

Thanks for doing this!

Something which throws me whenever I look at callback code, is where the "err, response, body" arguments to postResponse come from. I feel like this is a stumbling block for many people. Thanks again!

  var name = document.querySelector('input').value
  request({
    uri: "http://example.com/upload",
    body: name,
    method: "POST"
  }, postResponse)
}

function postResponse(**err, response, body**) { // <<<<<< here
  var statusMessage = document.querySelector('.status')
  if (err) return statusMessage.value = err
  statusMessage.value = body
}
document.querySelector('form').onsubmit = formSubmit

Curring and closures

An example could be:

app.get('something', function (req, res) {
  res.render('view1');
});

app.get('other', function (req, res) {
  res.render('view1');
});

Refactor to:

function respondWithView (viewName) {
 return function ( req, res ) {
   res.render(viewName);
 };
}

app.get('something', respondWithView('view1'));

app.get('other', respondWithView('view2'));

So why anonymous functions?

I sorta slept through web2.0 and when I got back into it EVERYONE was doing what Max calls callback hell so so did I. But it's more than just callback hell it's insane nesting. At the top you typically have a (function(){...}()) and everything inside ... is nested to the 27th degree. It's so easy to get confused about where a } or ; or , belongs that it can really waste your time compared to named functions.

I get it about keeping code out of the global namespace. I can also see some benefit in IDEs which collapse the various sections but are there any other reasons? Why did the JS developer community start using anonymous functions and "infinite" nesting?

Promises

The ultimate solution to all of your callback troubles. Sample implementation: when.js.

Other, more striking callback issues are not mentioned

Using callbacks comes with many issues and indentation with curly braces jungle is but one of them. By passing a callback you are passing the control over to a potentially buggy code with undesired behaviour.
The callback problems are:

  • callback can be called once, twice, infinitely many times, or never, and you have no control over it
  • callback can be called in synchronous or asynchronous fashion
  • callbacks swallow exceptions and force developers to conventions
  • callbacks make writing the curlybraced and indented code easy

I did not invent all of it myself, I learned a lot in YDKJS: https://github.com/getify/You-Dont-Know-JS/blob/master/async%20&%20performance/ch2.md

Yet the webpage only mentions the last one, from my point the least important one. Is it possible to at least mention that there are other, more serious problems, which actually turn this whole area into a real hell?

Illegal return statement

I think you should not write code as below:

if (err) return statusMessage.value = err

There will throw some error like: Uncaught SyntaxError: Illegal return statement

File Path:
callback-hell/index.html

async

I think more emphasis could be done on how to rewrite code from callback hell style to async style, also, more effor could be made in order to explain why async is a good idea, including async examples and await try catch stuff.

Missing semicolons

Where are the semicolons?!

I understand that it is intentional (whyyy), but since you are using vanilla JS it makes no sense to me.

The "Put a closure on it" section is very poor

First, the section has nothing to do with closures, at all. No variables are being closed over. It is maybe an example of factory functions, if anything.

Second, it has nothing to do with the rest of the page, and callback hell in general. It's more about someone's favorite pattern for decoupling DOM events from "business logic."

Finally, at the end it's trying to advertise hootroot.com, whatever that is.

IMO it doesn't belong on callbackhell.com.

Callback binding

Maybe it would be useful to mention callback binding, I know that personally was confusing for me in the beginning. For instance, you might expect this to work:

Something.prototype.foo = function (cb) {
  someOtherObj.bar(function (err, res) {
    cb(null, this.value)
  })
}

But actually it will not work since the innermost callback is called from the context of someOtherObj, and this doesn't resolve to the Something instance (so this.value end up being something else).

The solutions are foo(function () { this.value... }.bind(this)), var self = this; foo(function () { self.value... }), or maybe ES6 arrow functions (since they take their context from wherever they were created), () => { this.value... }.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.