I came upon a student solution that passed all the tests, and for the premium
function had something like:
premium(text, minimumQuality) {
return this.api.fetch(text)
.catch(error => {
this.request(text).catch(() => {});
return this.api.fetch(text);
})
.then( // ... rest of code
(The original version was more verbose than this, but functionally the same.)
At first glance it looks like a good solution: try fetch, interject with request, do fetch again.
But, the second fetch does not chain to request, and yet this passed all the tests.
The issue is two-fold: The success path passes because request
prepares the translation synchronously and delays just the reply, so an immediate fetch
that doesn’t wait for a reply can be successful.
For the expected failure path, the errors thrown from fetch
satisfy the tests’ toThrow(Error)
, since any child class of Error is itself an Error.
I’ve made changes (PR here) so that the api “work” is delayed in the callback, and introduced a new error type. I think this way it would work closer to a real life service api and as intended by the exercise assignment.