Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var connect = function(){
  2.   return new q(function(resolve, reject) {
  3.     var sqlCon = mysql.createConnection(connectionObject);
  4.     sqlCon.connect(function(err) {
  5.       if(err && err.fatal && err.code == "PROTOCOL_CONNECTION_LOST"){
  6.         return reject(err);
  7.       }
  8.       return resolve(sqlCon);
  9.     });
  10.   });
  11. };
  12.  
  13. var connectRecursive = function(){
  14.   var errorCount = 0;
  15.   return connect().catch(function(err) {
  16.     if (errorCount < 3) {
  17.       errorCount++
  18.     } else {
  19.       throw new Error('You failed too many times');
  20.     }
  21.     return connect();
  22.   });
  23. };
  24.  
  25. connectRecursive().then(function(connection) {
  26.   // do something with connection
  27. }).catch(function(err) {
  28.  
  29. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement