Well, it depends on what exactly you mean by “OK”. I am not aware of any laws restricting the use of the unsafeRunSync
method. I also don't think any major religion considers it a sin (though some minor ones certainly might).
That said, calling unsafeRunSync
is not referentially transparent and has all the downsides that come with that. Namely, equational reasoning goes out of the window. It also blocks the calling thread if asynchronous processing is involved at any point (and in ScalaJS it won't work at all in such cases). To me, these are sufficient reasons to avoid using it in production code whenever possible. That said, there are situations where it's not possible. For instance, you sometimes need to implement interfaces that expect side effects to be performed, such as java.io.OutputStream
. There's not much you can do in that situation, those signatures are the way they are.
It is however not necessary to call unsafeRunTimed
to perform an action with a timeout. Just use the race method and a Timer.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…