In this exercise you'll be providing a TranslationService
where paid members
have some quality assurance.
You have found a magical translation API that is able to fulfill any translation request in a reasonable amount of time, and you want to capitalize on this.
The magical API has a very minimal interface:
api.fetch(text)
fetches the translation of text
, returning two values:
translation
: the actual translationquality
: the quality expressed as a number
If there is no translation available (because it has not been requested yet), the API throws an error. This also happens if a piece of text is untranslatable.
api.request(text, callback)
requests the translation of text
, calling the
callback
once it's ready, without a value.
The request
API is unstable, which means that sometimes the API will call the
callback
with an error. If that happens, it is okay to re-request.
Because of some previous users being lazy when programming, always requesting a translation, without even checking if the text was already translated, the API returns an error if the text has already been translated ánd blocks all access completely, forever.
Implement a function to fetch a translation, ignoring the quality, and forwarding any errors thrown by the API:
service.free('jIyaj')
// => Promise<...> resolves "I understand."
service.free("jIyajbe'")
// => Promise<...> rejects Error("Not yet translated")
- Returns the translation if it can be retrieved, regardless its quality
- Forwards any error from the translation API
Implement a function that batch translates the given texts using the free service, returning all the translations, or a single error.
service.batch(['jIyaj', "majQa'"])
// => Promise<...> resolves ["I understand.", "Well done!"]
service.batch(['jIyaj', "jIyajbe'"])
// => Promise<...> rejects new Error("Not yet translated")
service.batch([])
// => Promise<...> rejects BatchIsEmpty()
- Resolves with all the translations (in the same order), if they are all available
- Rejects with the first error that is encountered
- Rejects with a
BatchIsEmpty
error if no texts are given
Implement a function that requests a translation, with automatic retries, up to a total of 3 calls for the same request.
service.request("jIyajbe'")
// => Promise<...> resolves (with nothing), can now be retrieved using the fetch API
Implement the function for premium users which fetch a translation, request it if it's not available, and only return it if it meets a certain threshold.
service.premium("jIyajbe'", 100)
// => Promise<...> resolves "I don't understand."
service.premium("'arlogh Qoylu'pu'?", 100)
// => Promise<...> rejects QualityThresholdNotMet()
service.premium("'arlogh Qoylu'pu'?", 40)
// => Promise<...> resolves "What time is it?"
The correct translation of 'arlogh Qoylu'pu'?
is How many times has it been heard?.