Created
August 12, 2018 01:48
-
-
Save JustinDFuller/1bdcf88233f2b7e1966775ebaa2497ec to your computer and use it in GitHub Desktop.
Using promise-funnel for authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import createFunnel from 'promise-funnel' | |
| const funnel = createFunnel() | |
| function fetchData(...options) { | |
| // This will fail if the user is unauthenticated | |
| return fetch(...options) | |
| } | |
| function login() { | |
| // all requests will be stopped while authentication is happening | |
| funnel.cork() | |
| // do login logic here | |
| // All the requests will happen now that the user is authenticated again | |
| funnel.uncork() | |
| } | |
| setTimeout(login, 60 * 5 * 1000 /* re-login every five minutes */) | |
| export default { | |
| fetchData: funnel.wrap(fetchData), | |
| login, | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment