Guest User

Untitled

a guest
Jan 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. /**
  2. * This function helps to make cancellable promise call maintaining object property cacellable.
  3. * On sucess, excutes a function if cancelled is false.
  4. * On failure, handle error if cancelled is false otherwise return error.
  5. *
  6. * api - promise to make call
  7. * parmas - input parameters for api
  8. * callAfterResponse - function to perform action using response
  9. * callAfterError - function to handle error if happens during excution of api
  10. **/
  11. export default function cancellableApiCall(api, params, callAfterResponse, callAfterError) {
  12. return {
  13. cancelled: false,
  14. cancelApicall: function () {
  15. this.cancelled = true;
  16. },
  17. callApi: async function callApi() {
  18. let response;
  19. try {
  20. response = await api(params);
  21. } catch (error) {
  22. if (typeof callAfterError === 'function') {
  23. !this.cancelled && callAfterError(error);
  24. }
  25. return error;
  26. }
  27. if (typeof callAfterError === 'function') {
  28. !this.cancelled && callAfterResponse(response);
  29. }
  30. return response;
  31. }
  32. }
  33. }
Add Comment
Please, Sign In to add comment