adverb
nearly; almost; on the verge of
Used when something bad or dramatic almost happened but was narrowly avoided. Often followed by a phrase describing the averted outcome, such as ~ところだった.
あわや大事故になるところだった。
It nearly turned into a major accident.
あわや衝突しそうになったが、間一髪で避けた。
We almost collided, but avoided it by a hair's breadth.
The origin is uncertain. It may be related to the interjection あわ (expressing surprise or alarm) combined with the particle や, but the exact derivation is unclear.