Skip to content

Instantly share code, notes, and snippets.

View llcoollasa's full-sized avatar

Lasantha llcoollasa

View GitHub Profile
@llcoollasa
llcoollasa / Delay example.ts
Created October 20, 2022 06:10
Await for some time when server returns 429
// Simple delay function using setTimeout and promise
export const delay = (milliseconds: number) => {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
};
// usage of delay function
async function getMeFoodAndDrinks(someParams: any, retries = 2): Promise<FoodAndDrinks[]> {
try {
return await getMeFoodAndDrinksFromStore(someParams);
@llcoollasa
llcoollasa / compare.js
Created November 2, 2019 08:02
Object Compare
var accountUsers = [
{_id:1, email:'email1'},
{_id:2, email:'email2'},
{_id:3, email:'email3'},
{_id:4, email:'email4'},
{_id:5, email:'email5'},
];
var calendarUsers = [
{_id:1, email:'email1', status: 'pending'},
@llcoollasa
llcoollasa / sample.ts
Created October 29, 2019 09:27
Resolving Promise inside map in Type Script
const A = await Promise.all(accountUsers.map(async (user) => {
const { state, csrfSecret } = await oauth.generateState();
return {
accountId: user.accountId,
firstName: user.firstName,
lastName: user.lastName,
email: user.email,
csrfSecret,
state,
@llcoollasa
llcoollasa / calendarUserRepository.spec.ts
Created October 23, 2019 09:45
Mongoose Mocking using Jest
jest.mock('mongoose');
import { model } from 'mongoose';
const calendarUserModelMock: any = {
find: jest.fn(() => calendarUserModelMock),
skip: jest.fn(() => calendarUserModelMock),
limit: jest.fn(() => calendarUserModelMock),
findByIdAndDelete: jest.fn(() => calendarUserModelMock),
};