David the prophet was accustomed to thanking God in the psalms, even before his request was answered, thanking Him at the same time he was presenting his request, confident that it would be answered.

Then he would continue in thanksgiving until the answer actually came, for faith and submission to the will of God were distinguishing features in David’s personality and life, not to mention the exceeding thanksgiving which he usually offered after his request was answered.

—H.E. Metropolitan Youssef, How to Pray