Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. let function_one(data) = function(){
  2. return new Promise( function(resolve, reject) {
  3. {do stuff...
  4. resolve('howdy');
  5. }
  6. });
  7. };
  8.  
  9. let function_two(some_data) = function(){
  10. return new Promise( function(resolve, reject) {
  11. { do stuff...
  12. resolve('howdy');
  13. }
  14. });
  15. };
  16.  
  17. let function_three(some_data) = function(){
  18. return new Promise( function(resolve, reject) {
  19. {do stuff...
  20. resolve('howdy');
  21. }
  22. });
  23. };
  24.  
  25. let function_four(some_data) = function(){
  26. return new Promise( function(resolve, reject) {
  27. {do stuff...
  28. resolve('howdy');
  29. }
  30. });
  31. };
  32.  
  33. function_one(data).then(function(result){
  34. return function_two(result);
  35. }).then(function(result){
  36. return function_three(result);
  37. }).then(function(result){
  38. return function_four(result);
  39. }).then(function(result){
  40. console.log(result);
  41. }).catch(err => console.log("Caught " + err));
  42.  
  43. let gettheinformation = function(invoicelist3){
  44. return new Promise( function(resolve, reject) {
  45. // console.log(invoicelist3);
  46. Product.aggregate([
  47. {
  48. $match: {
  49. Priced: true
  50. }
  51. }
  52. ,
  53. {
  54. $group: {
  55. _id: "$Invoice",
  56. pricedcount: {$sum: 1}
  57. }
  58. }
  59. ], function (err, result) {
  60. if (err) {
  61. console.log(err);
  62. } else {
  63. resolve(result);
  64. }
  65. });
  66. });
  67. };
  68.  
  69.  
  70. let getinvprodspriced = function(invlist){
  71. return new Promise( function(resolve, reject) {
  72. // console.log(invlist);
  73. for(var oo = 0; oo < invlist.length; oo++){
  74. Invoicestatus.update({Invoice:invlist[oo]._id}, {Noofitemspriced: invlist[oo].pricedcount}, {upsert: true}, function (err) {});
  75. }
  76. resolve(invlist);
  77. });
  78. };
  79.  
  80. let getinvprodcount = function(invprodcount){
  81. return new Promise( function(resolve, reject) {
  82. Product.aggregate([
  83. {
  84. $group: {
  85. _id: "$Invoice",
  86. pradcount: {$sum: 1}
  87. }
  88. }
  89. ], function (err, result) {
  90. if (err) {
  91. console.log(err);
  92. } else {
  93. // console.log(result);
  94. resolve(result);
  95. }
  96. });
  97.  
  98. });
  99. }
  100.  
  101.  
  102. let saveinvprodcount = function(invprodcount){
  103. return new Promise( function(resolve, reject) {
  104. for(var ok = 0; ok < invprodcount.length; ok++){
  105. Invoicestatus.update({Invoice:invprodcount[ok]._id}, {Noofitems: invprodcount[ok].pradcount}, {upsert: true}, function (err) {});
  106. }
  107. resolve(invprodcount);
  108. });
  109. };
  110.  
  111. let getarrdocs = function(result){
  112. return new Promise( function(resolve, reject) {
  113. Invoicestatus.find({}, function(err, docs){
  114. resolve(docs);
  115. });
  116. });
  117. };
  118.  
  119. router.get('/fetcharrdata', function(req, res) {
  120.  
  121.  
  122. gettheinformation().then(function(result){
  123. return getinvprodspriced(result);
  124. }).then(function(result){
  125. return getinvprodcount(result);
  126. }).then(function(result){
  127. return saveinvprodcount(result);
  128. }).then( function(result){
  129. return getarrdocs(result);
  130. }).then(function(result){
  131.  
  132.  
  133. res.json({status: 200, data: result});
  134.  
  135.  
  136. }).catch(err => console.log("Caught " + err));
  137.  
  138. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement