Guest User

Untitled

a guest
Jan 17th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. var res = "";
  2.  
  3.  
  4. function testComment(comment) {
  5. var words = comment.split(" ");
  6. words.forEach(function(word, i){
  7. (function(index){
  8. connection.query('select good_words from words where bad_words = ' + connection.escape(word), function(err, result){
  9. if(err){
  10. console.log(err);
  11. return;
  12. }
  13. if(result.length != 0){
  14. this.res = comment.replace(word, result[0].good_words);
  15. console.log("inside:" + this.res);
  16. }
  17. });
  18. })(i);
  19. });
  20. console.log("outside:" + this.res);
  21. return this.res;
  22. }
  23.  
  24. function callback(){
  25. return res;
  26. }
  27.  
  28. function testComment(comment, callback){
  29. ..all the working..(forEach loop which has a asyn function in it)
  30. return callback();
  31. }
  32.  
  33. var wordsProcessed = 0;
  34. (function(index){
  35. connection.query('select good_words from words where bad_words = ' + connection.escape(word), function(err, result){
  36. wordsProcessed++;
  37. if(err){
  38. console.log(err);
  39. return;
  40. }
  41. if(result.length != 0){
  42. this.res = comment.replace(word, result[0].good_words);
  43. console.log("inside:" + this.res);
  44. }
  45. if(wordsProcessed >= words.length){
  46. //THIS IS WHERE YOU RETURN YOUR RES
  47. }
  48. });
  49. })(i);
  50.  
  51. var Promise = require('bluebird');
  52. var promiseArray = words.map(function(word){
  53. return new Promise(function(resolve, reject){
  54. connection.query('select good_words from words where bad_words = ' + connection.escape(word), function(err, result){
  55. if(err) reject(err);
  56. else resolve(result);
  57. });
  58. });
  59. });
  60.  
  61. Promise.all(promiseArray).then(function(result){
  62. return this.res;
  63. });
Add Comment
Please, Sign In to add comment