Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. // Callback pattern, decomposition
  2.  
  3. const getSendResultsHandler = (cb) => (err, result) => {
  4. if (err) {
  5. console.error(`Failed to send results to mothership.`);
  6. cb(err);
  7. } else {
  8. console.log(`Message from the mothership: ${result}`);
  9. cb();
  10. }
  11. };
  12.  
  13. const getGatherResultsHandler = (cb) => (err, result) => {
  14. const sendResultsHandler = getSendResultsHandler(cb);
  15. if (err) {
  16. console.error(`Failed to get results, cancelling message.`);
  17. cb(err);
  18. } else {
  19. asyncDoSomethingWithResult(result, sendResultsHandler)
  20. }
  21. };
  22.  
  23. function reportToMotherShipCb(callback: GenericAsyncCallback<Error, never>) {
  24. const gatherResultsHandler = getGatherResultsHandler(callback);
  25.  
  26. asyncApi(gatherResultsHandler);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement