Hi Siraj,
Having just mentored my mom through that exercise, it feels like it should have a bunch of smaller lead-up exercises like the âElyse learns how to arrayâ set (but I canât complain too loudly without actually creating such a set⌠:)
=====
The Promise subsystem calls your .then(your_code) code from a catch block which will convert anything thrown into a rejected Promise containing the thrown data. So the two methods are approximately equivalent.
(Iâm not actually sure if it is that way, or the inverse: that if you return a rejected Promise, the caller will throw the rejected Promiseâs value.)
Either way, the two paths are unified.
=====
Promise.reject() simply creates a new already-rejected Promise bearing the value you passed. If you are supposed to be returning a rejected Promise, you need to return that value!
Otherwise it is just an ephemeral value you caused to be created, then discarded; not much different from a standalone statement: Math.max(3,4) â it calls that function, then drops the value on the floor. (However, Promises have additional semantics, so you will get some sort of reaction â but it probably will not be delivered to the place you meant to receive it!)
=====
Code which says return somePromise.then(...) is returning a Promise to the caller. If the function youâre returning from is an API function â that is, you are describing its call interface to other people, who will then use it in their code â you would have to tell them it returns a Promise. If they wanted a concrete value, you would have to make it so, e.g. retVal = await somePromise; return retVal (and document your API as returning a value).
In the âTranslation Serviceâ exercise you are creating a class with 4 available methods, each of which is defined, by the exercise instructions, as returning a Promise. So thatâs what you have to do.
In some parts of the exercise there are places where you might call .then() and not return or save the received value anywhere: if, in your code, you called the Promise constructor. Then you might have a .then((val) => resolve(val)), in which you are calling the resolve function passed to you by the constructor. Thatâs still a way of handling the value, but you neither stored it in a variable nor returned it.
=====
Boy itâs hard to talk about this stuff without getting longwinded! Sorry.