Skip to content

Instantly share code, notes, and snippets.

View jyotiarora2610's full-sized avatar

Jyoti Arora jyotiarora2610

View GitHub Profile
export async function fetchUtility({ url, headers, method }) {
try {
const response = await fetch(url, { ...headers }, method);
return response.json();
} catch(err) {
console.log("error", err);
}
}
@jyotiarora2610
jyotiarora2610 / soFetch.js
Created September 8, 2021 16:35 — forked from lusan/soFetch.js
Reusable fetch function from wesbos
// https://twitter.com/wesbos/status/1063515277911052290/photo/1
async function soFetch(input, settings = {}) {
const response = await fetch(input, {
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
...settings
});
let subscribers = {};
module.exports = {
publish(event, data) {
if (!subscribers[event]) return;
subscribers.forEach(callback => {
callback(data);
})
},
// Wrong way, bcoz variables are exposed.
// var foo = {
// o: {bar: 'fghj'},
// test() {
// console.log('hello');
// }
// }
// foo.test();
// console.log(foo.o);
// using bind
var sum = function(x, y) {
return x+y
}
var sumCall = sum.bind(this);
sumCall(3, 2);
var sumCall = sum.bind(this, 3);
sumCall(2);
let name = {
firstname: 'jyoti',
lastname: 'arora'
}
var printFullName = function (city, state) {
console.log(`${this.firstname} ${this.lastname} from ${city}, ${state}`);
}
var printMyName = printFullName.bind(name, 'MZN');
function throttle(func, wait) {
let flag = true;
return function() {
let context = this;
let args = arguments;
if (flag) {
func.apply(this, args);
flag = false;
}
else {
// for single object
function testFunction(a, b, c, d, {name, company, isUsingLaptop}) {
console.log(a, b, c, d)
console.log(name, company, isUsingLaptop)
}
var a = 10
var b = 20
var c = true
var d = false
function shiftArr (n, rotation, arr) {
for (let i= 0; i < rotation ; i++) {
let sElement = arr.shift()
arr.push(sElement)
}
}
shiftArr(5, 4, [1, 2, 3, 4, 5])
function candies(n, arr) {
var ans = 0
var values = []
values[0] = 1
for (let i=1; i< n; i++) {
if (arr[i-1] < arr[i]) {
values[i] = values[i-1] +1
} else {
values[i] = 1
}