yet another JS race condition fix

this should greatly reduce the number of timeouts seen in the JS tests
Merge pull request #4479 from ivanov/js-timeout

yet another JS race condition fix