Skip to content

Instantly share code, notes, and snippets.

View gowthm's full-sized avatar
:octocat:
Fly High

Gowtham gowthm

:octocat:
Fly High
View GitHub Profile
@gowthm
gowthm / debouncing.js
Last active October 4, 2025 12:46
Debouncing in Javascript
function debounce(fn, timer) {
let timeout;
return function(...arguments) {
clearTimeout(timeout);
timeout = setTimeout(() => {
fn.apply(this, arguments)
}, timer)
}
}
@gowthm
gowthm / creation_singleton.js
Last active October 17, 2023 21:41
Creational Design Pattern with example
// Singleton pattern
// SINGLEton. We use this design pattern when we only want a single instance of a class.
// That means we cannot create multiple instances - just one. If there is no instance, a new one is created.
// If there is an existing instance, it will use that one.
class Database {
constructor() {
this.connection = null;
}
static getDatabase() {
let a ={}
let b = {key: 'b'}
let c = {key: 'c'}
a[b] = 78
a[c] = 34
console.log(a[b])
---------------------------------------------------------------------------------------------------------------------------------------
@gowthm
gowthm / promiseVSnextTick.js
Created September 22, 2022 11:24
Compare promise and nexttick
process.nextTick(() => console.log(1));
Promise.resolve().then(() => console.log(2));
Promise.resolve().then(() => {
console.log(3);
process.nextTick(() => console.log(4));
Promise.resolve().then(() => console.log(5));
}).then(() => {
console.log(6);
})
@gowthm
gowthm / promiseMethods.js
Last active September 22, 2022 11:03
Here description all promise methods with examples.
// Promise.all
//will reject with this first rejection message
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("From Prmose1")
}, 1000)
})
const p2 = "From Promise2"
@gowthm
gowthm / countByEachString.js
Last active September 21, 2022 14:31
Javascript Programs.
let s = 'aaabbcddddswwaaaaa';
let c = 1;
let data = []
for (let i=0; i<s.length; i++) {
if (s[i] != s[i+1]) {
data.push(c);
data.push(s[i]);
c = 1;
} else {
c = c + 1;
@gowthm
gowthm / lexicalScope.js
Created September 15, 2022 18:23
This is related to scope, scope chain, block scope, lexical scope
let a = 10;
let func = function (){
let b = 20;
console.log("a and b is accessible (outer):", a, b);
let innerFunc= function (){
var c = 30;
console.log("a and b and c is accessible (innner):", a, b, c);
let innerFunc1 = function() {
console.log('Parent variable can accessible')
@gowthm
gowthm / arrowFunction.js
Last active September 15, 2022 17:49
Arrow function and different between regular function
// Regular Function with "this"
function Car() {
this.speed = 0;
this.speedUp = function(speed) {
this.speed = speed;
// let self = this; ----> without line anonymous function take as shadow from speedUp function.
setTimeout(function () {
console.log(self.speed)
}, 100)
@gowthm
gowthm / uploadFile.js
Last active December 20, 2020 11:08
Upload file by fs writeFile with Node Js
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}))
app.post('/upload-file', async function(req, res) {
const fileName = req.body.fileName;
const base64 = req.body.base64Data;