This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sleep(timeout) { | |
return new Promise(resolve => setTimeout(resolve, timeout)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function inParallel(list, fn) { | |
return Promise.all( | |
list.map(async (...args) => { | |
try{ | |
return { | |
status: true, | |
result: await fn(...args), | |
}; | |
}catch(error){ | |
return { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
OlderNewer