Confusion around Promises exercise in JS

Hello!

I don’t know enough about Promises in javascript to be sure, but there seems to be some inconsistency around the api.request function in the api class of the Translation Service exercise. In api.js, the fetch(text) function seems to always return a Promise, while request(text, callback) seems to use the pre-Promise style of passing in a callback function to return the result asynchronously. However, the example in the instructions for that exercise imply that the api.request function only takes the parameter text and returns a Promise. Lastly, the description of api.request in the instructions matches api.js and not the example.

What is the recommended approach to raise this kind of issue at Exercism?

(PS The hint also indicates that api.request is intended to be a callback-type function and not a Promise one.)

1 Like

Asking here is fine. As we move our focus from GitHub to the forum the response time from maintainers is probably a little slower, but it’ll come. As @kotp says, probably best to post this in the programming/javascript category (I’ll move it for you).

With regards to the content, I’ll let someone from the JavaScript team reply!

1 Like

@SleeplessByte @bobahop are you able to help out @DavidOfEarth on this issue?

  • ExternalApi#fetch always returns a Promise<Translation>.
  • ExternalApi#request is a callback style function that takes a callback (as second parameter)

You are tasked to implement a service that provides (among others) a free and request function, both of which always return promises.

Lastly, the description of api.request in the instructions matches api.js and not the example.

This is indeed a problem. I see these examples were added later and the top one is incorrect as api.request never returns a Promise (whereas service.request always does). I have supplied the following PR to resolve this:

(PS The hint also indicates that api.request is intended to be a callback-type function and not a Promise one.)

It is, so that will remain.

2 Likes

I have now rewritten the content for the promises exercise Translation Service:

2 Likes

Great piece of work. Some nitpicks, anyways:

In the rejection example an arrow is missing. It should read:

// Creates a promise that is immediately rejected
new Promise((_, reject) => {
  reject(reason)
})

In the example section, the catch comment refers to the wrong reason string. It ought to be:

  .catch(function (reason) {
    // If the random number was 4 or 5, this will be reached and
    // reason will be "Sampling did not result in a sample". The entire chain will
    // then reject with an Error with the reason as message.
    throw new Error(reason);
  })

Good examples are hard to make up. The way the random number is generated is one of the problems we discuss with student about evenly distributed random numbers. 0 and 5 are not generated as often as expected. Better distribution has:

const randomNumber = Math.floor(Math.random() * (5 + 1));

Again: It’s a great piece of work, that made me understand Promise much better than ever before!

2 Likes

I recommend you to try this exercise as well. It’s a classic JS interview question topic

1 Like

Thanks. Do you want to PR the fixes to the first two things?

[…] are not generated as often as expected.

This is “intentional” in the sense that this version is much easier to understand than a more “fair” implementation using the PRNG.