fileWriter.write()
is an asynchronous operation. You basically created a race here.
What happens without the timeouts (or even with the timeouts if the writes are slow):
receiveFile(0)
fileWriter.seek(fileWriter.length /* == 0 */)
queue write(0)
receiveFile(1)
fileWriter.seek(fileWriter.length /* == 0 */)
- Yeah, still0
as there was no actual write performed yet.queue write(1)
- ...
actual async write(0)
actual async write(1)
- ...
The rest of what happens is down the implementation details and bugs. So in conclusion: Always wait for write operations to finish before doing another write.
The current spec draft actually disallows calling seek/write
when readyState === WRITING
, and states that readyState
should be set to WRITING
as soon as the write
function gets called.
Chrome does not implement this version of the spec yet, or is still buggy, or else if would have caught your race and thrown InvalidStateError
on the second call to .seek()
(timeout-less version).