We often have to work with so many different APIs in a project so what if we are currently calling all three APIs at once and the token expires? Then all three API requests will call for a token refresh, but only one API will call the token refresh successfully and the other two APIs will fail because the token refresh mechanism will cancel the expiring token as soon as it is refreshed.
SOLVE –request 1–> (token expired detection) –request 2–> (later requests must wait for the token to return) –request 3–> (no matter how many requests you have to wait)
The following code simulates the programming process when token expires
// Result // Request api with token expired: /me Current token // Api request new token: /me // Request api with token expired: /products Current token // Request api with token expired: /posts Current token
// Request api: /me This is new token // Request api: /products This is new token // Request api: /posts This is new token