Skip to content

Instantly share code, notes, and snippets.

View srebalaji's full-sized avatar
🎯
Focusing

Srebalaji Thirumalai srebalaji

🎯
Focusing
View GitHub Profile
@srebalaji
srebalaji / .vimrc
Last active October 30, 2017 06:14
Vim configuration
set number
set noet ci pi sts=0 sw=4 ts=4
set cursorline
set showcmd
filetype indent on
set wildmenu
set lazyredraw
set showmatch
set incsearch
set hlsearch
@srebalaji
srebalaji / Gemfile
Last active May 5, 2019 04:55
Pry plugins
pry-rescue
pry-nav
pry-rails
pry-stack_explorer
pry-theme
@srebalaji
srebalaji / sample-promise.js
Created March 4, 2019 16:57
A simple JS file that explains Promise.all
// A simple promise that resolves after a given time
const timeOut = (t) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Completed in ${t}`)
}, t)
})
}
// Resolving a normal promise.
@srebalaji
srebalaji / sample-promise-in-map.js
Last active March 23, 2019 07:43
A sample promise with map
// A simple promise that resolves after a given time
const timeOut = (t) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Completed in ${t}`)
}, t)
})
}
const durations = [1000, 2000, 3000]
@srebalaji
srebalaji / sample-promise-all-in-rejection.js
Last active March 23, 2019 07:50
A sample promise all with rejection
// A simple promise that resolves after a given time
const timeOut = (t) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (t === 2000) {
reject(`Rejected in ${t}`)
} else {
resolve(`Completed in ${t}`)
}
}, t)
@srebalaji
srebalaji / sample-promise-all-handle-rejection.js
Last active April 11, 2019 05:08
A sample promise all to handle rejections
const durations = [1000, 2000, 3000]
promises = durations.map((duration) => {
return timeOut(duration).catch(e => e) // Handling the error for each promise.
})
Promise.all(promises)
.then(response => console.log(response)) // ["Completed in 1000", "Rejected in 2000", "Completed in 3000"]
.catch(error => console.log(`Error in executing ${error}`))
@srebalaji
srebalaji / Promise all example to send email.js
Last active April 11, 2019 05:07
A simple Promise all example to send emails
// Async function to send mail to a list of users.
const sendMailForUsers = async (users) => {
const usersLength = users.length
for (let i = 0; i < usersLength; i += 100) {
const requests = users.slice(i, i + 100).map((user) => { // The batch size is 100. We are processing in a set of 100 users.
return triggerMailForUser(user) // Async function to send the mail.
.catch(e => console.log(`Error in sending email for ${user} - ${e}`)) // Catch the error if something goes wrong. So that it won't block the loop.
})
@srebalaji
srebalaji / promise-all-map.js
Last active February 9, 2024 17:14
A sample Promise.all in map
// Function to fetch Github info of a user.
const fetchGithubInfo = async (url) => {
console.log(`Fetching ${url}`)
const githubInfo = await axios(url) // API call to get user info from Github.
return {
name: githubInfo.data.name,
bio: githubInfo.data.bio,
repos: githubInfo.data.public_repos
}
}
@srebalaji
srebalaji / example.js
Last active April 6, 2020 09:49
NodeJS Worker thread example
// index.js
const {Worker, isMainThread, parentPort, workerData} = require('worker_threads')
const worker = new Worker("./worker.js", {workerData: {a: 5, b: 10}})
worker.once('message', (result) => {
console.log('The sum is', result)
})
@srebalaji
srebalaji / main.js
Last active April 7, 2020 07:47
NodeJS Worker thread example
const {Worker, isMainThread, parentPort, workerData} = require('worker_threads')
// Returns if the script runs in main thread of in worker
if (isMainThread) {
const arr = [[1, 10],[2, 15],[3, 21],[4, 25],[5, 86]]
for (const ele of arr) {
const worker = new Worker(__filename, {workerData: {a: ele[0], b: ele[1]}})
worker.on('message', (result) => {
console.log(`The sum of ${ele[0]} and ${ele[1]} is ${result}`)