While solving the javascript/translation-service exercise, my solution to the batch()
led to one of the tests, 'it throws if one or more translations fail'
, behaving erratically: it sometimes failed, sometimes succeeded.
This seemed to occur both locally (with node 18) and using the online editor.
The original code was:
batch(texts) {
if (texts.length === 0) {
return Promise.reject(new BatchIsEmpty());
}
return texts.reduce(
(accPromise, text) => this.api.fetch(text)
.then(
api_response => accPromise.then(
accResult => Promise.resolve([...accResult, api_response.translation])
)
)
, Promise.resolve([]));
}
I think I understand how this fails, and that’s besides the point (“flipping” the promises to chain up on the accumulator promise, instead, works fine).
The problem is that the test should probably always fail in this scenario.
I’m too green to javascript (and I don’t really plan to ripen too much either) to venture to suggest a solution.
Please let me know if there’s a better place to raise this.