Guest User

Untitled

a guest
Jun 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. function getOrdersByUserId(userId, month = 4) {
  2. const apiService = new ApiService();
  3.  
  4. return apiService.getOrdersList(`?OrderingUser=${userId}`)
  5. .then(orders => {
  6. const monthOrders = orders.filter(order => new Date(order.FromTime)
  7. .getMonth() === month);
  8.  
  9. return monthOrders;
  10. });
  11. }
  12.  
  13. getOrdersList(queryString = '') {
  14. return httpsRequest.createRequest(this.URL.ordersList + queryString, {}, this.requestHeaders, 'GET')
  15. .then(result => JSON.parse(result).Records);
  16. }
  17.  
  18. const promises = userIds.map(userId => {
  19. return getOrdersByUserId(userId, month)
  20. .then(orders => {
  21. return orders;
  22. });
  23. });
  24.  
  25. Promise.all(promises).then(results => {
  26. console.log(results);
  27. }).catch(err => {
  28. console.log(err);
  29. });
  30.  
  31. (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): SyntaxError: Unexpected end of JSON input
  32.  
  33. const results =
  34. userIds.reduce((acc, userId, index) => {
  35. acc.p =
  36. p.then(getOrdersByUserId(userId, month))
  37. .then(orders => {
  38. return acc.results[index] = orders;
  39. });
  40. return acc;
  41. }, {p: Promise.resolve(), results: []})
  42. .then(acc => acc.results)
  43. .then(results => {
  44. // `results` is an array of results, in the same order as `userIds`
  45. })
  46. .catch(err => {
  47. console.log(err);
  48. });
Add Comment
Please, Sign In to add comment