Skip to content

Instantly share code, notes, and snippets.

View cherihung's full-sized avatar

Cheri Hung cherihung

View GitHub Profile
let cache;
const TTL = 1500;
const callApi = () => {
return new Promise((res, rej) => res('1234'))
}
async function getData() {
if (cache) return cache;
try {
const newProm = await callApi();
console.log('get new result', newProm);
@cherihung
cherihung / allSettled.js
Created February 20, 2022 00:25
allSettled implementations
async function success() {
return new Promise((res, rej) => res('Resolved'));
}
async function error() {
return new Promise((res, rej) => rej('Rejected'));
}
// then, catch syntax
function allSettled(promises) {
const allPromises = promises.map((p) => {
@cherihung
cherihung / createLargeFile.js
Created March 21, 2021 23:30
generate any large file
const fs = require('fs');
const path = require('path');
const bytes_100 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mauris augue, vulputate sit amet dolor.\n';
const bytes_400 = bytes_100.repeat(4)
const fileName = 'file.txt';
const file = fs.createWriteStream(path.join(__dirname, fileName));
for (let i = 0; i <= 1e6; i++) {
file.write(bytes_400);
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/cherihung/go-server-starter/config"
package config
import (
"fmt"
"path"
"github.com/spf13/viper"
)
var appConfigs AppConfiguration
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
# 1 choose a compiler OS
FROM golang:alpine AS builder
# 2 (optional) label the compiler image
LABEL stage=builder
# 3 (optional) install any compiler-only dependencies
RUN apk add --no-cache gcc libc-dev
WORKDIR /workspace
# 4 copy all the source files
COPY . .
# 5 build the GO program
const compose = (...functions) => x => functions.reduce((acc, fn) => fn(acc), x);
// compose(fn1, fn2, fn3)
const pipe = functions => data => {
return functions.reduce(
(value, func) => func(value),
data
);
};
@cherihung
cherihung / generate_min_hours.js
Last active July 21, 2017 15:44
generate string array of time units
const _generator = i => {
return i < 9 ? '0'+(i+1) : (i+1).toString();
};
const UNITS = Array.from({length: 60}, (v, i) => {
return _generator(i);
});
@cherihung
cherihung / check-for-current.html
Last active June 25, 2017 02:25
for jekyll templates - determine if item in menu is the current one navigated to
{% for item in sorted_items %}
{% assign current = nil %}
{% if page.id == item.url %}
{% assign current = 'currentItem' %}
{% endif %}
<li class="{{current}}"><a href="{{item.url}}"><img src="{{item.imageId}}.jpg" /></a></li>
{% endfor %}