Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Can I get these issues resolved, perhaps?
Tests should be a little more logical now
Random tests added
Aha, I get you! Thank you for laying it out. I can't think of a way to test that an error is not thrown in place while simultaneously handling the unhandled rejection. The best I could come up with is
Should handle thrown errors as rejections
where it tests that an error thrown in the generator is turned into a rejection.Luckily, I can get a passing test for
Should not throw in place
and Codewars doesn't much care about unhandled rejections.I've also randomised much of the data in the test fixture.
As for async generators, I specifically wanted to emulate async/await with plain old generators. I should emphasise that in the description!
I could perhaps still do a better job of explaining in description; is there anything else I'm missing?
Thanks a lot for your assistance on my first kata.
EDIT: Oh I just thought of some nicer ways to randomise that would help prevent some of the brute forcing. Will update shortly.
The fundamental problem here is, the kata's premise is flawed: synchronous and asynchronous JS handle exceptions being thrown in place differently. When a non-
async
function throws in place it throws an exception in the current execution context; this cannot happen inasync
function as the entire function is assumed to be wrapped in aPromise
.This has very, very, very important ramifications on how to handle exceptions throwing from them. Synchronous exception are handled via
try
/catch
. Asynchronous exception can only be handled via.catch
. And you can only turn the former into the latter, but not the opposite:Similarly,
asynk
is taking a generator that yieldsPromise
s rather than anAsyncGenerator
, which also has this difference:More technically, code inside
async
/Promise
isn't run in the same "context": everyasync
/Promise
queues the function in the event loop to be run in a different "context" later. So the current expectation from the kata is really unsound inside the JS runtime model, and is definitely incorrect.And besides all this, it doesn't help that the description only says "behave as similarly to async/await as is feasible", which doesn't explain how everything as mentioned above should work. Fixed tests only give part of the spec; the other part is in the test fixture, which nobody can see.
Hello, yes
asynk
is supposed to return a Promise whether the last yielded/returned value from the generator is a Promise or not. The generator yields Promises to theasynk
function. You're right it's not an async generator, it's just a generator.The generator can
return
a Promise too, just like async functions can return a Promise. Or they can return values which are wrapped in Promises. Both of these async functions returns a Promise of 1:I want to highlight that the yields shouldn't be returned from
asynk
. Both of these should give you a Promise ofundefined
:I will, however, update the tests to be more logical, and since async functions allow you to
await
a non-Promise for some reason, perhaps I should allow that too?Please give me some pointers, as I'm new to writing tests.
Random tests are needed
The kata design is illogical:
If
asynk
is supposed to be async, it should return aPromise
and be handled in usual ways (.then
/.catch
). Async errors aren't thrown to the current point of execution.A generator returning a
Promise
isn't async code at all; it is a generator, not an async generator. The execution point stays the same scope, and returns to the same place when something isyield
ed/return
ed from the generator.So why is the generator returning
Promise
s? Does the kata author even know how async/await and generators work?