Skip to content

Instantly share code, notes, and snippets.

View Deepak13245's full-sized avatar
🎯
Focusing

Deepak Kumar Deepak13245

🎯
Focusing
View GitHub Profile
@Deepak13245
Deepak13245 / connector.js
Last active May 24, 2020 16:26
Weather API connector
import Axios from "axios";
const connector = Axios.create({
baseURL: "https://cors-anywhere.herokuapp.com/https://www.metaweather.com/api"
});
export const getWeatherByLocationId = async id => {
const { data } = await connector.get(`/location/${id}/`);
return data;
};
@Deepak13245
Deepak13245 / WithoutHook.js
Created May 24, 2020 16:27
Weather without hooks
import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import WeatherCard from './WeatherCard';
import { getWeatherByLocationId } from "./connector";
function WithoutHook({ locationId }) {
const [data, setData] = useState(null);
async function reload() {
try {
import {useState, useEffect} from 'react';
import {getWeatherByLocationId} from './connector';
function useWeather(locationId){
const [data, setData] = useState(null);
async function reload() {
try {
setData(null);
setData(await getWeatherByLocationId(locationId));
import React from "react";
import PropTypes from "prop-types";
import WeatherCard from './WeatherCard';
import useWeather from "./weatherHook";
function WithHook({ locationId }) {
const [data, reload] = useWeather(locationId);
return (
<WeatherCard title="With Hooks" data={data} reload={reload} />
@Deepak13245
Deepak13245 / inSeries.js
Created June 2, 2020 18:54
Run async functions in series
function inSeries(list, fn) {
return list.reduce(async (acc, ...rest) => {
const results = await acc;
try{
results.push({
status: true,
result: await fn(...rest),
});
}catch(error){
results.push({
@Deepak13245
Deepak13245 / sleep.js
Created June 2, 2020 19:01
Sleep function in javascript
function sleep(timeout) {
return new Promise(resolve => setTimeout(resolve, timeout));
}
@Deepak13245
Deepak13245 / utils.js
Last active July 7, 2021 19:22
Few js utilities
async function measure(promise) {
const start = Date.now();
try {
const result = await promise;
return result;
} catch(e) {
throw e;
} finally {
const time = Date.now() - start;
console.log(`Time taken ${time} ms`);
@Deepak13245
Deepak13245 / inParallel.js
Created June 2, 2020 19:17
Running async functions in parallel
function inParallel(list, fn) {
return Promise.all(
list.map(async (...args) => {
try{
return {
status: true,
result: await fn(...args),
};
}catch(error){
return {
@Deepak13245
Deepak13245 / optimizeImage.js
Last active June 3, 2020 21:16
Optimize jpeg and png images
const imagemin = require('imagemin');
const imageminJpegTran = require('imagemin-jpegtran');
const imageminPngQuant = require('imagemin-pngquant');
const imageminJpegRecompress = require('imagemin-jpeg-recompress');
const fs = require('fs');
async function optimizeImageBuffer(buffer) {
return imagemin.buffer(buffer, {
plugins: [
imageminJpegRecompress({
@Deepak13245
Deepak13245 / inParallelWithLimit.js
Created June 2, 2020 20:17
Run async functions in parallel with limited amout of concurrency.
async function inParallelWithLimit(list, concurrency, fn) {
// chunk is imported from here :- https://gist.github.com/Deepak13245/129ecdbb72c9f0f81827eb421260a8da
const batches = chunk(list, concurrency);
// Process batches in series
// inSeries :- https://gist.github.com/Deepak13245/c39c67c45abbf5d2a357fba5beeabbde
// inParallel :- https://gist.github.com/Deepak13245/e49c1853ebceacbd3ab6f725c42b4ba6
const data = await inSeries(batches, async (items, batch) => {
// Process items in a batch in parallel
return inParallel(items, fn);