Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var res = "";
- function testComment(comment) {
- var words = comment.split(" ");
- words.forEach(function(word, i){
- (function(index){
- connection.query('select good_words from words where bad_words = ' + connection.escape(word), function(err, result){
- if(err){
- console.log(err);
- return;
- }
- if(result.length != 0){
- this.res = comment.replace(word, result[0].good_words);
- console.log("inside:" + this.res);
- }
- });
- })(i);
- });
- console.log("outside:" + this.res);
- return this.res;
- }
- function callback(){
- return res;
- }
- function testComment(comment, callback){
- ..all the working..(forEach loop which has a asyn function in it)
- return callback();
- }
- var wordsProcessed = 0;
- (function(index){
- connection.query('select good_words from words where bad_words = ' + connection.escape(word), function(err, result){
- wordsProcessed++;
- if(err){
- console.log(err);
- return;
- }
- if(result.length != 0){
- this.res = comment.replace(word, result[0].good_words);
- console.log("inside:" + this.res);
- }
- if(wordsProcessed >= words.length){
- //THIS IS WHERE YOU RETURN YOUR RES
- }
- });
- })(i);
- var Promise = require('bluebird');
- var promiseArray = words.map(function(word){
- return new Promise(function(resolve, reject){
- connection.query('select good_words from words where bad_words = ' + connection.escape(word), function(err, result){
- if(err) reject(err);
- else resolve(result);
- });
- });
- });
- Promise.all(promiseArray).then(function(result){
- return this.res;
- });
Add Comment
Please, Sign In to add comment