This file contains hidden or 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
/** | |
* Provides a class for managing retry operations. | |
* @class | |
*/ | |
module.exports = class ExponentialBackoffRetry { | |
/** | |
* Constructor. | |
* | |
* @param {Object} [options] The options. | |
* @param {number} [options.baseDelay] The base delay in milliseconds. Defaults to 1000. |
This file contains hidden or 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
// As stated before "readyState" is good. "ping" is also good admin utility for doing so as well. | |
// It will return { ok: 1 } if it can accept commands. | |
const mongoose = require('mongoose'); | |
// Create the database connection. | |
const connection = await mongoose.createConnection( | |
CONNECT_URI, | |
CONNECT_OPTS | |
); |
This file contains hidden or 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
{ | |
"Horizontal Spacing" : 1, | |
"Tags" : [ | |
], | |
"Ansi 12 Color" : { | |
"Red Component" : 0.50197118520736694, | |
"Color Space" : "sRGB", | |
"Blue Component" : 0.9097973108291626, | |
"Alpha Component" : 1, |
This file contains hidden or 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
" Specify Vim-Plug installation folder | |
call plug#begin('~/.vim/plugged') | |
Plug 'tpope/vim-sensible' | |
" Install the Nightfly color scheme plugin | |
Plug 'zacanger/angr.vim' | |
Plug 'jacoborus/tender.vim' | |
Plug 'vim-airline/vim-airline' | |
Plug 'vim-airline/vim-airline-themes' |
This file contains hidden or 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
/* | |
* Provides a powerful tool for implementing custom iterators and | |
* simplifying asynchronous workflows. Generators make it easier to | |
* handle complex iteration logic and asynchronous processes, leading | |
* to more readable and maintainable code. They can also be used for | |
* tasks like managing asynchronous operations in a more | |
* straightforward, linear fashion. | |
*/ | |
function* objectEntries(obj) { | |
for (let key of Object.keys(obj)) { |
This file contains hidden or 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
/* The Proxy object allows you to create a proxy for another object, | |
* enabling you to intercept and redefine fundamental operations such | |
* as property lookup, assignment, enumeration, function invocation, | |
* etc. This provides a powerful way to add custom behavior to objects. | |
*/ | |
const user = { | |
name: 'John', | |
age: 30 | |
}; |
This file contains hidden or 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
/* This code utilizes the “cluster” module to create multiple worker processes, | |
* each handling incoming requests. This approach allows you to scale your | |
* application horizontally to handle increased traffic. Note that they all use | |
* the same port. | |
*/ | |
const cluster = require('cluster'); | |
if (cluster.isMaster) { | |
// Master process forks worker processes | |
for (let i = 0; i < numCPUs; i++) { |
This file contains hidden or 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
/* This code utilizes “cacheable-request” to cache the response from the | |
* specified URL for 60 seconds. Subsequent requests within that timeframe | |
* will retrieve the data from the cache, reducing API calls and improving | |
* responsiveness. | |
*/ | |
const cacheableRequest = require('cacheable-request'); | |
const cachedRequest = cacheableRequest( | |
requestFn, | |
{ ttl: 60000 } // Cache for 60 seconds |
This file contains hidden or 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
/* This code creates a worker thread from the “long_calculation.js” file. The main | |
* thread sends data to the worker, which performs the calculation and returns the | |
* result via the “ message ” event. This approach keeps your main thread responsive | |
* while intensive tasks run in the background. | |
*/ | |
const { Worker } = require('worker_threads'); | |
const worker = new Worker('./long_calculation.js'); | |
worker.on('message', (result) => { |
This file contains hidden or 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
/* The streamlined operator (“ ?. ”) acts as a nullish coalescing operator. It checks if | |
* the preceding value is “ null ”or “ undefined ” before attempting to access the next | |
* property. This simplifies conditional checks and streamlines your code, making it more | |
* readable and less error-prone. | |
*/ | |
async function getUser(userId) { | |
const user = await db.getUser(userId); | |
// Traditional approach (prone to errors with undefined values) | |
if (user && user.profile && user.profile.avatarUrl) { | |
return user.profile.avatarUrl; |
NewerOlder