That is what chapter 38 does for us in the middle of the story of Joseph. The last section of Genesis, which starts in chapter 37 is about Joseph and his faith and integrity in the God of his forefathers, Abraham, Isaac, and Jacob. But the writer of Genesis, Moses, throws in this story of Judah for us to read and ponder. The Judah in chapter 38 is there to contrast the Judah of chapter 44 along with the Joseph of chapter 39. Even more important for us to remember in reading this is the fact that Judah was in the line of David and Jesus. This man if sexual promiscuity and unfaithfulness is in the family tree of the Savior. By this unexpected interruption of the story of Joseph, Genesis and the original readers were kept in suspense. We must wait a little longer to discover what happens to our man Joseph after he is sold in the slavery.
But the story of Judah and Tamar is not irrelevant to the main course of the whole narrative. This was not added as a footnote but also teaches us the theology of Genesis and continues the themes and phrases of the rest of the story. It is concerned with how the promises of the descendants for the patriarchs should be fulfilled. It shows how the hard-hearted Judah was stopped in his tracks and then prepares us to meet a different Judah later on the in the story. It even gives us another set of twins in the picture of the younger overtaking the older. This story of Judah has a purpose and reason and when compared to Joseph begs to ask us the question.
Are we more like Judah or more like Joseph?