Onanism has nothing to do with masturbation
What Onan did wasn't masturbation, it was breaking the law and denying his sister-in-law any economic stability. That's his sin. That's why god murdered him. He refused to take care of his family, a childless widow who, at the time, could not support herself or own anything without a husband or a son.
To put it more simply: We have a responsibility to take care of each other.
That use of the story abuses the text worse than any juvenile boy has ever abused himself. Onan wasn’t masturbating — he was having sex with his sister-in-law. The story cannot be twisted into teaching that masturbation puts one in danger of being put to death by divine intervention. (Apart from contradicting the text, the idea that anyone who masturbates might be struck dead by God is obviously wrong anyway — disproved by the continuing existence of the human race.)
It’s equally mendacious to abuse this story by trying to force it to say something else it refuses to say: that sex must always be for the purpose of procreation. That’s not what the text says. That’s not something the story itself will allow you to say this story “teaches.” The story absolutely does not say that sex must always be for the purpose of procreation. The story says, rather, that sex with your dead brother’s childless widow must always be for the purpose of procreation.
And to understand what that’s all about in this story, we have to discuss the howling anachronism here.
. . .