Guest User

Untitled

a guest
Jul 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. class Test {
  2.  
  3. constructor() {
  4. this.processData({}).then((data) => {
  5. console.log(data);
  6. })
  7. }
  8.  
  9. async processData(data) {
  10. data = await this.one(data);
  11. console.log('one');
  12. data = await this.two(data);
  13. console.log('two');
  14. data = await this.three(data);
  15. console.log('three');
  16. return data;
  17. }
  18.  
  19. one(data) {
  20. return new Promise((resolve, reject) => {
  21. setTimeout(() => {
  22. data.foo = 11;
  23. data.bar = 42;
  24. resolve(data);
  25. }, 2000);
  26. });
  27. }
  28.  
  29. two(data) {
  30. return new Promise((resolve, reject) => {
  31. setTimeout(() => {
  32. if (data.hasOwnProperty('foo')) {
  33. Object.keys(data).forEach((key) => {
  34. data[key] = 2 * data[key];
  35. });
  36. resolve(data);
  37. } else {
  38. reject('wtf');
  39. }
  40. }, 500);
  41. })
  42. }
  43.  
  44. three(data) {
  45. return new Promise((resolve, reject) => {
  46. setTimeout(() => {
  47. data.qux = Object.keys(data).reduce((result, key) => {
  48. return result + data[key];
  49. }, 0);
  50. resolve(data);
  51. }, 1000);
  52. })
  53. }
  54.  
  55. }
  56.  
  57. module.exports = new Test();
Add Comment
Please, Sign In to add comment