0

I have a few different code paths with multiple callbacks.

this.confirmJWT(
  function() {
    this.confirmVendorCode(
      function() {
        this.downloadFile(FORECAST);
      }.bind(this)
    );
  }.bind(this)
);

I'm aware that people call this callback hell (actually just found http://callbackhell.com/ ), but I'm looking for the preferred way to write this browser code.

Blair Anderson
  • 19,463
  • 8
  • 77
  • 114
  • 1
    If you don't tell us what the code is doing (e.g. by showing the implementations of the used functions), we cannot suggest to change anything without the risk of breaking the functionality. – Bergi Oct 26 '17 at 18:46
  • 2
    Probably a better question for [Code review](https://codereview.stackexchange.com/) – Cjmarkham Oct 26 '17 at 18:46

1 Answers1

1

Writing it using ES6 would make it shorter, perhaps not as readable though.

this.confirmJWT(() => this.confirmVendorCode(() => this.downloadFile(FORECAST)));
Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
  • The context of `this` is different in arrow functions versus function declarations. Is there any way to know that this won't break functionality? – Dan Mandel Oct 26 '17 at 18:50
  • 3
    @DanMandel, since arrow functions just inherit `this` from their parent, and OP's example is binding parent `this`, they are both using the same `this` – Ozan Oct 26 '17 at 18:52
  • yeah this does make it look nicer to write the anonymous functions. – Blair Anderson Oct 26 '17 at 21:40