Struggling with Javascript Promises (Translation Service) - Request a translation

I’ve been at the problem for several hours now, and I still can’t wrap my head around it.

So far, what I understand about the problem is:

  • I am supposed to use the api.request function to return either: 1) an error from the API, or 2) nothing. I will call this function in the form of this.api.request(text, callback) where text is the argument in service.request, and I will define a callback function in the body of service.request.

  • In the case of outcome (1), I am supposed to try at most 2 more times, and if I still get an error on the last try, I will return the last error I received. This error is in the form of a Promise object that has been rejected, and this Promise object will throw the erorr that I received from the API.

  • In the case of outcome (2), I will return a Promise object that has been resolved, and this Promise object will be undefined (equivalent to Promise.resolved(undefined)).

However, my issue now is that I’m unable to evaluate the api.request function to error or not error. From reading the hint, I know that I’m supposed to convert the callback function to a promise using the new Promise constructor, but I’m not sure how to do so in a way that doesn’t cause the “tests failed to run” error.

Based on reading the API code itself, it seems that the callback function is only called in the event of an error, and that the error is the argument of the callback function. Am I interpreting this correctly?

So far, my code looks like this:

  request(text) {
    function callback(input) {
        const callbackPromise = new Promise((resolve,reject) => {
          throw input;
        })
        return callbackPromise.then().catch((err) => err);
    }
    var requestTranslation = this.api.request(text,callback);
    console.log(`requestTranslation: ${requestTranslation} | ${typeof requestTranslation}`)
    if (requestTranslation === undefined) {
      return Promise.resolve(undefined);
    } else return requestTranslation;
  }

I haven’t tried implementing the retries yet because I’m still stuck on how to resolve one try.

Any other guidance would be greatly appreciated. Thanks in advance.

Hi @gammafrontier,

Welcome to the Exercism forum! :smiley:

This is a tricky exercise which I struggled with a lot myself. I’d be happy to mentor you on this problem, if you’d like to please open a request and let me know here and I’ll accept it.

A couple of comments on your understanding so far:

From reading the hint, I know that I’m supposed to convert the callback function to a promise using the new Promise constructor

Are you sure you need to convert the callback function to a promise?

Based on reading the API code itself, it seems that the callback function is only called in the event of an error, and that the error is the argument of the callback function.

What makes you think the callback function is only called in the event of an error?

What do we expect to be returned from our request method? Take a look at the method’s JSDoc for a clue.

I hope this gives you some pointers in the right direction and as I said, happy to do a mentor session on this one with you should you wish.

2 Likes

@alexmitchelldev FYI, if the student can’t get the tests passing they can’t request a mentor session and are pointed here instead.

Is this new? I have been mentored on a previous exercise where I wasn’t able to get all the tests to pass.

Nope. It’s been the case forever. However, if you submit via the CLI you bypass that requirement. But most people use the Online Editor in which case you can’t do it. It’s to stop the numerous very basic support requests we get from flooding the queues. But for situations such as this one, where someone has successfully navigated a few exercises, the intention is to allow them to do it. But it’s a bit of a code-upheaval so it’s not happened yet.

Thanks for the explanation Jeremy!

1 Like

Hi, OP here! Sorry for the late response.

@alexmitchelldev, thank you so much for your guiding questions! They helped me to relook the documentation and figure out where my understanding was wrong, as well as helped to set me on the right path. However, I’m still stuck on one final test that I can’t seem to get passing. I’ve installed and submitted my incomplete solution via the CLI (thanks @iHiD for pointing out that it’s possible - I’ve been using the online editor all this time) and requested a mentoring session on my code.

1 Like