0

I am trying to understand a block of code here, which I believe is redundant code, which could be deleted but maybe I am wrong, here is the code:

return(
    myModule.getSomething(args.url)
    .then(stream => module.uploadData({
      param1: args.param1,
      param2: args.param2,
      param3: stream,
    }))
    .then(() => myBroker.dispatch({
        queueUrl: myQueueUrl,
        payload: JSON.stringify(args.payload),
      })
    )
    .then(msgInfo => {}) .  //This line can be removed right?
    .catch(error => {
      myBroker.dispatch({
        queueUrl: anotherQueueUrl,
        payload: JSON.stringify({ type: 'error', payload: `[ERROR] ${error}` }),
      });

      throw error;
    })
  );

I believe the line:

.then(msgInfo => {})

can be deleted right? the arrow function does nothing, the msgInfo is something that the previous then clause returns after calling myBroker.dispatch, and because of that, I could remove the line I mentioned right?

Can someone give me a clear explanation on this? Why that line exists? Or explain why can't I delete it?

PS: the function getSomething and uploadData returns a promise, the dispatch function I am not so sure because it looks like this:

return(
    client.sendMessage(parameters).promise()
    .then(data => data)
  );

It returns only a object which is 'data' right? or does it return a promise as well, because it contains the then clause in the end?

This syntax notation using multiple 'then' and arrow functions confuses me,

Thanks a lot in advance!

TiagoM
  • 3,458
  • 4
  • 42
  • 83
  • It is not clear why the line exists. Yes, the line can be removed. – guest271314 Feb 22 '19 at 08:58
  • The function doesn't do nothing, it returns an empty object, which eventually your top level return statement will use as its return value. Clarification, your top level return returns a promise which will resolve to { } when all is said and done. – JSager Feb 22 '19 at 08:58
  • You can use promise.all for handling multiple api calls like in [here](https://stackoverflow.com/questions/33073509/promise-all-then-resolve) – Mukundhan Feb 22 '19 at 08:59
  • 1
    "The function doesn't do nothing, it returns an empty object," @JSager. Nope. It returns undefined. Curly brackets create a block here, not object literal. So this function "swallows" `msgInfo` and makes the promise to be resolved to `undefined` instead. – Yury Tarabanko Feb 22 '19 at 09:07
  • I thought if you had a single arg lambda with no parentheses and a block scope declared you get an invalid syntax error. – JSager Feb 22 '19 at 09:15
  • Huh. You're right. ESLint would explode on me if I tried that though. – JSager Feb 22 '19 at 09:16

1 Answers1

1

The only reason I can think of for why that line would exist would be if the function this whole block is being used in is somewhere such that the resolve value of myBroker.dispatch should not be (or is better not) exposed to the consumer of the larger function. For example:

const thisModule = (() => {
  const myBroker = ...;
  function getSomethingAndDispatchBroker() {
    return(
      myModule.getSomething(args.url)
      .then(stream => module.uploadData({
        param1: args.param1,
        param2: args.param2,
        param3: stream,
      }))
      .then(() => myBroker.dispatch({
        queueUrl: myQueueUrl,
        payload: JSON.stringify(args.payload),
      }))
      .then(msgInfo => {})
      .catch(error => {
        myBroker.dispatch({
          queueUrl: anotherQueueUrl,
          payload: JSON.stringify({ type: 'error', payload: `[ERROR] ${error}` }),
        });
        throw error;
      })
    );
  }
  return { getSomethingAndDispatchBroker };
})();

Here, the function getSomethingAndDispatchBroker can be called from outside, but perhaps the resolve value of myBroker.dispatch is better kept internal to the thisModule - either due to privacy, or due to code clarity - perhaps you only want users of the function to know if the Promise resolved (if the process succeeded), without telling them unnecessary details.

Still, if none of that is a concern, then yes, feel free to remove the .then(msgInfo => {}) line.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Thank you for your detailed answer! I noticed that the line can actually be removed, there is no need to have it on my case. – TiagoM Feb 22 '19 at 09:39