Skip to content

Instantly share code, notes, and snippets.

🎯
Focusing

Srebalaji Thirumalai srebalaji

🎯
Focusing
View GitHub Profile
@srebalaji
srebalaji / git-hard-delete
Last active Jun 18, 2020
Examples of git custom command
View git-hard-delete
#!/bin/sh
branch=$1
if [ ! -z "$1" ]
then
git branch -D $branch
git push -d origin $branch
else
echo "Branch name is not specified"
@srebalaji
srebalaji / readme.md
Created Apr 11, 2020
How to fix React router redirection in webpack production build - Netlify
View readme.md
@srebalaji
srebalaji / workerPool.js
Last active Apr 8, 2020
Worker thread pool - Manage worker threads pool in NodeJS
View workerPool.js
// workerPool.js
// Worker Thread pool
const {Worker, parentPort, MessageChannel} = require('worker_threads')
class WorkerPool {
constructor(size, worker) {
this.size = size
this.worker = worker
this.pool = []
@srebalaji
srebalaji / main.js
Created Apr 7, 2020
Message channels between workers
View main.js
// main.js
const {Worker, parentPort, MessageChannel} = require('worker_threads')
const worker = new Worker('./worker.js')
const messageChannel = new MessageChannel()
// As you can see there are two parameters passed in `postMessage`
worker.postMessage({yourPort: messageChannel.port1}, [messageChannel.port1])
@srebalaji
srebalaji / main.js
Created Apr 7, 2020
Message channel in Worker threads
View main.js
const { MessageChannel } = require('worker_threads')
const {port1, port2} = new MessageChannel()
port1.once('message', (msg) => {
console.log(msg)
})
port2.postMessage({msg: "hello world"})
@srebalaji
srebalaji / main.js
Last active Apr 7, 2020
NodeJS Worker thread example
View main.js
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}`)
@srebalaji
srebalaji / example.js
Last active Apr 6, 2020
NodeJS Worker thread example
View example.js
// 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 / promise-all-map.js
Last active Mar 23, 2019
A sample Promise.all in map
View promise-all-map.js
// 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 / Promise all example to send email.js
Last active Apr 11, 2019
A simple Promise all example to send emails
View Promise all example to send email.js
// 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 / sample-promise-all-handle-rejection.js
Last active Apr 11, 2019
A sample promise all to handle rejections
View sample-promise-all-handle-rejection.js
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}`))
You can’t perform that action at this time.