1. 14
  1. 4
    1. 4

      Do people really cheat with their javascript formatting like that? That seems like it would be very misleading to read and I would not encourage it in any language.

      1. 2

        Mostly no. At least not now - e.g. it tends to blow up as soon as a source code formatter goes anywhere near it. Alternatives are more widely encouraged in the community.

        1. async/await
        2. promises
        3. other APIs such as https://caolan.github.io/async/v3/

        The async/await thing is the simplest one for flattening code:

        async function doTheNeedful() {
          const resultA = await a();
          const resultB = await b(resultA);
          try {
            await c(resultB);
          } catch(error) {

        The promises API often lets you flatten code to a fixed indentation depth (usually 2 or 3). This isn’t as nice as async because sometimes the clearest way to lay out code doesn’t follow the layout that’s easiest to write in with manual use of promises.

        function doTheNeedful2() {
          return a()
            .then(function(resultA) {
              return b(resultA);
            .then(function(resultB) {
              return c(resultB)
                .catch(function(error) {
        // oh no it crept over to the right a bit. maybe try doing something different?
        function doTheNeedful3() {
          function afterA(resultA) { return b(resultA).then(afterB); }
          function afterB(resultB) { return c(resultB).catch(catchC); }
          function catchC(error) { window.alert("Nooo"); }
          return a().then(afterA);
        // but now the order of statements is all weird and meh - maybe we should've just accepted the extra indentation levels in the earlier version

        There exist other APIs than promises, such as the old “async” library you’ll find on npm https://caolan.github.io/async/v3/ which give you a library of higher order functions for composing callback-based asynchronous functions together. I do not personally want to use those.