News for hackers. Stuff that matters.
Re:How to chain multiple functions in JavaScript properly wi... by nikodunk on Monday August 12, 2019 @11:01PM (#20682425)
Hey gang!

I've always found it easier to think through async operations with .then(), but recently decided to make the switch. Totally worth it for conciseness! I thought I'd share my learnings above.

Suggestions welcome!

Re:How to chain multiple functions in JavaScript properly wi... by cprecioso on Tuesday August 13, 2019 @03:11AM (#20683482)
Hey, something is wrong with your interpreter or your transpiler if you need to wrap every `async` function in a `return new Promise`. If you mark a function as `async` it will do it automatically - in fact, if you look at e.g. `nodent`'s output, it's exactly how it does it. Calling the `resolve` function instead a `then` callback like that is a mess, and totally not needed. Also, the `return` is not necessary, every function has an implicit `undefined` return if not explicitly stated.

Moreover, in async functions you can take advantage of regular `try`/`catch` when awaiting; it seems you're just swallowing errors in your functions, which I guess is fine for the code you're showing. But keep in mind that a better practice is to let those function throw and catch it with a try/catch at the callsite.

Lastly, when calling the functions to save and send the email, you can use `Promise.all` and parallelize them instead of running them serially, as they don't seem to depend on each other. If you're meaning to send the email only after it is saved, then you need to check the return value of the saving before that; because neither of the calls will ever fail; just return undefined.

Re:How to chain multiple functions in JavaScript properly wi... by nikodunk on Tuesday August 13, 2019 @04:03PM (#20689667)
Ah, excellent points. Thank you for the very carefully crafted comment. I will update once I've re-tested with these changes.
Re:How to chain multiple functions in JavaScript properly wi... by kwxza on Friday August 16, 2019 @04:24AM (#20713177)
Forgive me for this simple and unrelated question, but I'm new to JS development. This line right here, what is this?

let courseId = fields.to.substring(0, fields.to.indexOf('@'));

I've never seen this 'to' string method and I can't find a doc page for it. Can you explain it or point me towards a reference? Thanks

Re:How to chain multiple functions in JavaScript properly wi... by stevula on Friday August 16, 2019 @04:35PM (#20719454)
I haven't read the article, but there's no special significant to the `to` here. It is just looking up the value of a key named `to` in an object called `fields`, using [dot notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...):

  const fields = {
    to: "[email protected]"
  };

  fields.to  // "[email protected]