You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow node.js routines to reconnect if not connected
Basic Code Example
this is a good pattern!
the code before:
constclient=redis.createClient({url: 'redis://localhost:6379'});client.connect().catch(err=>{log.error('redis could not connect:',err);});
async functionperformOp(key: string,value: string): Promise<bar>{try{constresult=awaitclient.set(key,value,{NX: true,// Only set the key if it does not already exist});
the code after:
constclient=redis.createClient({url: 'redis://localhost:6379'});client.connect().catch(err=>{log.error('redis could not connect:',err);});(clientasany).ensure=async()=>{// <<< this oneif(client.isOpen){return;}returnclient.connect();}asyncfunctionperformOp(key: string,value: string): Promise<bar>{try{await(clientasany).ensure();// <<< THIS ONEconstresult=awaitclient.set(key,value,{NX: true,// Only set the key if it does not already exist});
this is a really good pattern that has served me well in multiple languages/runtimes over the years. Sure we might still get an error, but it will be a simple subset of the existing way.
thanks
The text was updated successfully, but these errors were encountered:
Motivation
Allow node.js routines to reconnect if not connected
Basic Code Example
this is a good pattern!
the code before:
the code after:
this is a really good pattern that has served me well in multiple languages/runtimes over the years. Sure we might still get an error, but it will be a simple subset of the existing way.
thanks
The text was updated successfully, but these errors were encountered: