Last active
June 19, 2018 00:28
-
-
Save kselax/cf16d5aaf17dae2b47cfa5129b88fe50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse_cookies(req, next){ | |
// funciton for working with promises | |
let promise = function(name){ | |
console.log('name===' + name); | |
return new Promise((resolve, reject) => { | |
client.get(name, function(err, reply) { | |
console.log('name = ' + name + ' reply=' + reply); | |
resolve('name = ' + name + ' reply=' + reply); | |
}); | |
}); | |
} | |
if(req.headers.cookie != null){ | |
var result = req.headers.cookie.match(new RegExp('m:[^=]*=[^; ]*', 'ig')); | |
console.log('result= '+result); | |
if(result != null){ | |
// array of promisess | |
var mass_prom = []; | |
// work with our cookie | |
for(var i = 0; i < result.length; i++){ | |
console.log('i = ' + i); | |
console.log('result[i] = ' + result[i]); | |
var result1 = result[i].split('='); | |
result1[0] = result1[0].substr(2); | |
console.log('result1[0] = ' + result1[0]); | |
console.log('result1[1] = ' + result1[1]); | |
var name = 'sess:'+result1[0]; | |
// run promise | |
mass_prom.push(promise(name)); | |
} // for for getting promises | |
console.log(mass_prom); | |
Promise.all(mass_prom).then(function(values) { | |
console.log('values = ' + values); | |
for(var i=0; i<values.length; i++){ | |
console.log('i= ' + i + ' values[i]= ' + values[i]); | |
} | |
next(); | |
}); | |
// for for hadling then | |
// for( var i = 0; i < mass_prom.length; i++ ) { | |
// console.log(i); | |
// mass_prom[i].then((successMessage) => { | |
// // successMessage is whatever we passed in the resolve(...) function above. | |
// // It doesn't have to be a string, but if it is only a succeed message, it probably will be. | |
// console.log('i = ' + i + " Yay! " + successMessage); | |
// // next(); // pass next middleware | |
// }); | |
// } | |
} | |
} | |
// | |
// | |
// myFirstPromise.then((successMessage) => { | |
// // successMessage is whatever we passed in the resolve(...) function above. | |
// // It doesn't have to be a string, but if it is only a succeed message, it probably will be. | |
// console.log("Yay! " + successMessage); | |
// next(); // pass next middleware | |
// }); | |
} // parse_cookies |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment