Skip to content

Instantly share code, notes, and snippets.

View benweizhu's full-sized avatar
🇨🇳
I may be slow to respond.

Ben benweizhu

🇨🇳
I may be slow to respond.
View GitHub Profile
@benweizhu
benweizhu / clean-dynamo-table
Created May 23, 2018 10:08 — forked from toshke/clean-dynamo-table
Truncate all keys in a dynamodb table
#!/bin/bash
####
#### Delete (remove) all items from Aws Dynamo DB table, by specifing table name and primary key
####
#### Forked from https://gist.github.com/k3karthic/4bc929885eef40dbe010
####
#### Usage:
#### clean-dynamo-table TABLE_NAME PRIMARY_KEY
####
@benweizhu
benweizhu / Promise.reject.js
Created May 11, 2018 05:28
How to reject a promise from inside then function
new Promise(function (resolve) {
resolve(1);
}).then(function () {
return Promise.reject(new Error('error'))
}).then(function () {
}).catch(function (error) {
console.log(error)
})
@benweizhu
benweizhu / reduce-chain.js
Last active May 11, 2018 03:18
promises reduce to promise chain
const array = [1, 2, 3, 4, 5];
array
.reduce(function(promise, currentValue, currentIndex) {
return promise
.then(function() {
return new Promise(function(resovle) {
setTimeout(function() {
console.log("send", currentValue);
resovle(currentValue);
@benweizhu
benweizhu / promise-resolve-in-sync-order.js
Last active May 4, 2018 06:39
promise resolve in order sync
const array = [1, 2, 3, 4, 5];
new Promise(function(resolve, reject){
const outSideResolve = resolve;
array.reduce(function (promise, currentValue, currentIndex) {
return promise.then(function () {
return new Promise(function (resovle, reject) {
setTimeout(function () {
console.log(currentValue, currentIndex);
resovle(currentValue);
@benweizhu
benweizhu / promise.js
Created April 27, 2018 08:34
Wrap XMLHttpRequest to Promise
function fetchData(URL) {
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', URL, true);
req.onload = function () {
if (req.status === 200) {
resolve(this.responseText);
} else {
reject(new Error(req.statusText));
}
@benweizhu
benweizhu / webpack.config.js
Created April 2, 2018 08:03
webpack node js
const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
module.exports = {
target: "node",
entry: "./index.js",
externals: [nodeExternals()],
output: {
libraryTarget: 'commonjs2',
path: path.resolve(__dirname, ""),
@benweizhu
benweizhu / nginx.conf.default
Created January 30, 2018 10:02 — forked from nishantmodak/nginx.conf.default
Default Nginx Conf
#user nobody;
#Defines which Linux system user will own and run the Nginx server
worker_processes 1;
#Referes to single threaded process. Generally set to be equal to the number of CPUs or cores.
#error_log logs/error.log; #error_log logs/error.log notice;
#Specifies the file where server logs.
@benweizhu
benweizhu / gist:e3d2e65cafc05d010e431410a9a50fce
Created January 23, 2018 07:07
Dockerfile for GOCD Install Pip and fix Locale missing error
FROM gocd/gocd-agent-ubuntu-16.04:v17.11.0
RUN apt-get clean && apt-get update && apt-get install -y locales
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
RUN apt-get update && \
@benweizhu
benweizhu / noncritcss.md
Created July 26, 2017 09:40 — forked from scottjehl/noncritcss.md
Comparing two ways to load non-critical CSS

I wanted to figure out the fastest way to load non-critical CSS so that the impact on initial page drawing is minimal.

TL;DR: Here's the solution I ended up with: https://github.com/filamentgroup/loadCSS/


For async JavaScript file requests, we have the async attribute to make this easy, but CSS file requests have no similar standard mechanism (at least, none that will still apply the CSS after loading - here are some async CSS loading conditions that do apply when CSS is inapplicable to media: https://gist.github.com/igrigorik/2935269#file-notes-md ).

Seems there are a couple ways to load and apply a CSS file in a non-blocking manner:

@benweizhu
benweizhu / app.js
Created July 20, 2017 08:38 — forked from acdlite/app.js
Quick and dirty code splitting with React Router v4
// getComponent is a function that returns a promise for a component
// It will not be called until the first mount
function asyncComponent(getComponent) {
return class AsyncComponent extends React.Component {
static Component = null;
state = { Component: AsyncComponent.Component };
componentWillMount() {
if (!this.state.Component) {
getComponent().then(Component => {