Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const pred = () => Math.random() > 0.5;
- const authSuccess = () => new Promise(
- ( resolve, reject ) => pred() ? resolve(200) : reject('Network Error')
- ); // produce network errors just to be more realistic
- const authFail = () => new Promise(
- ( resolve, reject ) => pred() ? resolve(401) : reject('Network Error')
- ); // omitting the Response interface as it's not needed in the example
- const wrapped = net => options => new Promise(( resolve, reject ) => net(options).then(
- code => code !== 401 ? resolve('Data') : reject('Unauthorized'), // this is not necessarily the only then() in chain
- reject
- ));
- console.clear(); // useful in repls
- wrapped(authSuccess)().then(console.log, console.log); // using console.log as simplest effect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement