Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. var userArray = [
  2. {
  3. name: "John",
  4. email: "John@email.com"
  5.  
  6. },
  7. {
  8. name: "Jane",
  9. email: "jane@email.com"
  10. }];
  11.  
  12. var functionOne = function() {
  13. //returns Promsie object
  14. };
  15.  
  16. var functionTwo = function() {
  17. //returns promise object
  18. };
  19.  
  20. var createUser = function(user) {
  21. return User.findOrCreate({email: user.email},{
  22. name: user.name,
  23. email: user.email
  24. });
  25. };
  26.  
  27. functionOne()
  28. .then(functionTwo)
  29. .each(createUser(userArray))
  30. .then(function onComplete() {
  31. console.log("Complete");
  32. })
  33. .catch(function onError() {
  34. console.log("Um...it's not working");
  35. });
  36.  
  37. var Promise = require('bluebird');
  38.  
  39. function createUsersFromArray(userArray){
  40. return Promise.each(userArray, function(signleUser){
  41. return createUserFunction(signleUser);
  42. });
  43. }
  44.  
  45. return Promise.each(userArray, createUserFunction);
  46.  
  47. functionOne()
  48. .then(functionTwo)
  49. .then(function(){
  50. return createUsersFromArray(userArray);
  51. })
  52. //or just .then(createUsersFromArray) if functionTwo return this array
  53. .then(function(createdUsers){
  54. //here you may retrieve users and make some magic with them
  55. console.log(createdUsers);
  56. })
  57. .then(function onComplete() {
  58. console.log("Complete");
  59. })
  60. .catch(function onError() {
  61. console.log("Um...it's not working");
  62. });
  63.  
  64. return Promise.all(userArray.map(function(singleUser){
  65. return doSomethingWithUser(singleUser);
  66. }));
  67.  
  68. return Promise.all(userArray.map(doSomethingWithUser));
  69.  
  70. functionOne()
  71. .then(functionTwo)
  72. .then(function(){
  73. return bluebird.each(userArray, createUser);
  74. })
  75. .then(function onComplete() {
  76. console.log("Complete");
  77. })
  78. .catch(function onError() {
  79. console.log("Um...it's not working");
  80. });
  81.  
  82. var userArray = [
  83. {
  84. name: "John",
  85. email: "John@email.com"
  86.  
  87. },
  88. {
  89. name: "Jane",
  90. email: "jane@email.com"
  91. }];
  92.  
  93. var functionOne = function() {
  94. //returns Promise object
  95. };
  96.  
  97. var functionTwo = function() {
  98. //returns Promise object
  99. };
  100.  
  101. var createUser = function(singleUser) {
  102. //returns Promise object containing creating User
  103. };
  104.  
  105. functionOne()
  106. .then(functionTwo)
  107. .then(function() {
  108. return Promise.map(userArray, createUser);
  109. })
  110. .then(function onComplete(response) {
  111. console.log("Complete:" + JSON.stringify(response));
  112. })
  113. .catch(function onError() {
  114. console.log("Um...it's not working");
  115. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement