From Genesis 38-40
Gen 38.1-30; Story; Tamar pretends to be a prostitute to continue the family line
Gen 39.1-23; Story; Joseph resists potiphar's wife and is thrown in prison
Gen 40.1-23; Story; Joseph interprets the dreams of two prisoners sowing seeds of his release
Passage and comments
Each of these stories continues to demonstrate a common underlying plot. That of Gods continued faithfulness to his covenant to Abraham and his offspring (Gen 12, 15, 17).
The incident between Judah and Tamar for instance seems a strange break with the main plot. But Jewish hearers of the story will know through Judah's line the covenant blessings will pass, David will be born, and the promised Christ, Jesus will come. This story, despite its confronting presentation of the characters, is another testament to God's providence.
Joseph sold into slavery prospers, is cast down again, then prospers again. God is with him. There is an explicit reference to God's covenant love in this story as well.
 As soon as his master heard the words that his wife spoke to him, “This is the way your servant treated me,” his anger was kindled.  And Joseph's master took him and put him into the prison, the place where the king's prisoners were confined, and he was there in prison.  But the LORD was with Joseph and showed him steadfast love and gave him favor in the sight of the keeper of the prison. (Gen 39:19-21)
People who know Josephs story know its ending and what I'm referring too. As a foretaste for those who don't. God is bringing the story of Joseph to a much beneficial outcome for Gods family and using it to set up another bigger story.
Consider also, despite the colourful nature of the characters in these stories, how they have been and are now remembered. The Jews afterwards esteem Judah very highly. Through him kings will come.Yet the story portrays him as quite uncompromising on prostitution despite his own dalliance. Why? Two reasons I suspect. The value attached to God's covenant blessing's and devotion to family outweigh these blemishes. Jacob predicts;
 “Judah, your brothers shall praise you;
your hand shall be on the neck of your enemies;
your father's sons shall bow down before you.
 Judah is a lion's cub;
from the prey, my son, you have gone up.
He stooped down; he crouched as a lion
and as a lioness; who dares rouse him?
 The scepter shall not depart from Judah,
nor the ruler's staff from between his feet,
until tribute comes to him;
and to him shall be the obedience of the peoples. (Gen 49:8-10)
Judah becomes highly esteemed by association.
Story of JesusEventually through Judah was born Jesus the Christ. Joseph preserved the family line so that this could happen. And through them both, God remained faithful to his covenant promises to Abraham and his offspring. The same Jesus that was born some two thousand years ago, lived, died on a cross for our sins and rose again on the third day. Jesus said he will come again and judge. Gods family believe God’s promises.