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 / 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 / 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-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.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 / .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