Onanism is usually defined as the improper use of the sexual faculties; that is, for sexual pleasure without being open to the possibility of life.
It doesn’t matter how “Onanism” is “usually defined”. What matters is the context of the Biblical passage where the sin of Onan is discussed.
As previously mentioned, the context of Genesis 38 is that Onan failed to honor his family duties by providing offspring for his deceased brother. That passage doesn’t provide a blanket condemnation of withdrawing during sex.