Guest User

Untitled

a guest
Sep 14th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. Node.js For-Loop Inner Asynchronous Callback
  2. isPasswordInPwdHistory : function(redisClient, userId, encPassword, cb) {
  3.  
  4. var key = 'user:' + userId + ':pwdhistory';
  5. redisClient.llen(key, function(err, reply) {
  6. if (err) {
  7. status.results = [ ];
  8. xutils.addStatusResultsItem(status.results, err, null, null, null);
  9. cb(status);
  10. }
  11. else {
  12. var numPwds = reply;
  13.  
  14. var match = false;
  15. var funcError;
  16.  
  17. for (i=0; i <= numPwds - 1; i++) {
  18. var error;
  19. var pwdValue;
  20. redisClient.lindex(key, i, function(err, reply) {
  21. if (err) {
  22. console.log('lindex err = ' + err);
  23. error = err;
  24. }
  25. else {
  26. console.log('lindex reply = ' + reply);
  27. console.log('lindex encPassword = ' + encPassword);
  28. console.log('lindex (encPassword === reply) = ' + (encPassword === reply));
  29. pwdValue = reply;
  30. }
  31. });
  32.  
  33. console.log('for-loop error = ' + error);
  34. console.log('for-loop pwdValue = ' + pwdValue);
  35. console.log('for-loop (encPassword === pwdValue) = ' + (encPassword === pwdValue));
  36.  
  37. if (error) {
  38. funcError = error;
  39. break;
  40. }
  41. else if (encPassword === pwdValue) {
  42. console.log('passwords match');
  43. match = true;
  44. break;
  45. }
  46. }
  47.  
  48. console.log('funcError = ' + funcError);
  49. console.log('match = ' + match);
  50.  
  51. if (funcError) {
  52. status.results = [ ];
  53. xutils.addStatusResultsItem(status.results, err, null, null, null);
  54. cb(status);
  55. }
  56. else
  57. cb(match);
  58. }
  59. });
  60. }
  61.  
  62. for-loop error = undefined
  63. for-loop pwdValue = undefined
  64. for-loop (encPassword === pwdValue) = false
  65. funcError = undefined
  66. match = false
  67. isPasswordInPwdHistory = false
  68. lindex reply = 5f4f68ed57af9cb064217e7c28124d9b
  69. lindex encPassword = 5f4f68ed57af9cb064217e7c28124d9b
  70. lindex (encPassword === reply) = true
  71.  
  72. isPasswordInPwdHistory : function(redisClient, userId, encPassword, cb) {
  73.  
  74. var status = new Object();
  75. var key = 'user:' + userId + ':pwdhistory';
  76.  
  77. redisClient.llen(key, function(err, reply) {
  78. if (err) {
  79. status.results = [ ];
  80. xutils.addStatusResultsItem(status.results, err, null, null, null);
  81. cb(status);
  82. }
  83. else {
  84. var numPwds = reply;
  85. var loopCt = 0;
  86.  
  87. for (i=0; i <= numPwds - 1; i++) {
  88. loopCt++;
  89. redisClient.lindex(key, i, function(err, reply) {
  90. if (err) {
  91. status.results = [ ];
  92. xutils.addStatusResultsItem(status.results, err, null, null, null);
  93. cb(status);
  94. }
  95. else if (encPassword === reply) {
  96. status.results = [ ];
  97. xutils.addStatusResultsItem(status.results, null, 0, null, true);
  98. cb(status);
  99. }
  100. else if (loopCt === numPwds && encPassword !== reply) {
  101. status.results = [ ];
  102. xutils.addStatusResultsItem(status.results, null, 0, null, false);
  103. cb(status);
  104. }
  105. });
  106. }
  107. }
  108. });
  109. }
  110.  
  111. var numPwds = reply;
  112. ...
  113. var match = false;
  114. var i = 0;
  115.  
  116. async.whilst(
  117. // keep looping until we have a match or we're done iterating the list
  118. function () { return match == false && i < numPwds; },
  119.  
  120. // this is our function to actually check Redis
  121. function (callback) {
  122. redisClient.lindex(key, i, function(err, reply) {
  123. if (err) {
  124. console.log('lindex err = ' + err);
  125. error = err;
  126. }
  127. else {
  128. if (encPassword === reply) { match = true; } // this will cause `whilst` to stop.
  129. }
  130. i++;
  131. callback();
  132. });
  133. },
  134.  
  135. function (err) {
  136. // this is called when our first function that calls
  137. // return match == false && i < numPwds
  138. // returns false, which will happen when we're out of list elements to check
  139. // or we have a match. At this point, match being true or false
  140. // let us know if we found the password.
  141. }
  142. );
Add Comment
Please, Sign In to add comment