Advertisement
Guest User

Untitled

a guest
May 24th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. const pred = () => Math.random() > 0.5;
  2.  
  3. const authSuccess = () => new Promise(
  4. ( resolve, reject ) => pred() ? resolve(200) : reject('Network Error')
  5. ); // produce network errors just to be more realistic
  6.  
  7. const authFail = () => new Promise(
  8. ( resolve, reject ) => pred() ? resolve(401) : reject('Network Error')
  9. ); // omitting the Response interface as it's not needed in the example
  10.  
  11. const wrapped = net => options => new Promise(( resolve, reject ) => net(options).then(
  12. code => code !== 401 ? resolve('Data') : reject('Unauthorized'), // this is not necessarily the only then() in chain
  13. reject
  14. ));
  15.  
  16. console.clear(); // useful in repls
  17.  
  18. wrapped(authSuccess)().then(console.log, console.log); // using console.log as simplest effect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement