Skip to content

Instantly share code, notes, and snippets.

@vsashyn
vsashyn / Init webpack + babel test project
Last active May 20, 2020 10:30
Configure webpack + babel simple project. Watch mode + source maps.
// CLI
// 1. yarn init
// 2. yarn add @babel/core @babel/preset-env babel-loader webpack webpack-cli webpack-dev-server --dev
// 3. Add webpack.config.js
// 4. Add babel.config.js
// 5. Add script '"watch": "webpack-dev-server"
// Enjoy <3
@vsashyn
vsashyn / async-iterators-example.js
Created November 16, 2018 10:21 — forked from DmitrySoshnikov/async-iterators-example.js
async-iterators-example.js
/**
* Async iterators simple example.
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License, 2018
*/
async function* streamChunks() {
yield genChunk(1);
yield genChunk(2);
@vsashyn
vsashyn / Middleware.js
Created June 29, 2018 10:33 — forked from darrenscerri/Middleware.js
A very minimal Javascript (ES5 & ES6) Middleware Pattern Implementation
var Middleware = function() {};
Middleware.prototype.use = function(fn) {
var self = this;
this.go = (function(stack) {
return function(next) {
stack.call(self, function() {
fn.call(self, next.bind(self));
});
@vsashyn
vsashyn / nodejs-custom-es6-errors.md
Created June 20, 2018 12:08 — forked from slavafomin/nodejs-custom-es6-errors.md
Custom ES6 errors in Node.js

Here's how you could create custom error classes in Node.js using latest ES6 / ES2015 syntax.

I've tried to make it as lean and unobtrusive as possible.

Defining our own base class for errors

errors/AppError.js

@vsashyn
vsashyn / cancelablePromise.js
Created May 1, 2018 10:36
Cancelable promise with proper handling exception on main promise
// cancelable promise
// originaly by @istarkov
const makeCancelable = (promise) => {
let hasCanceled_ = false;
const wrappedPromise = new Promise((resolve, reject) => {
promise.then((val) =>
hasCanceled_ ? reject({isCanceled: true}) : resolve(val),
error => console.warn
const fs = require('fs')
const path = require('path')
const os = require('os')
const walk = function (dir, done) {
let results = []
fs.readdir(dir, function (err, list) {
if (err) return done(err)
let pending = list.length
if (!pending) return done(null, results)
@vsashyn
vsashyn / curl.md
Created May 15, 2017 14:52 — forked from subfuzion/curl.md
curl POST examples

Common Options

-#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.

-b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).

-c, --cookie-jar <file name> File to save response cookies to.

@vsashyn
vsashyn / destructuring.js
Created May 11, 2017 12:31 — forked from mikaelbr/destructuring.js
Several demos and usages for ES6 destructuring. Runnable demos and slides about the same topic: http://git.mikaelb.net/presentations/bartjs/destructuring
// === Arrays
var [a, b] = [1, 2];
console.log(a, b);
//=> 1 2
// Use from functions, only select from pattern
var foo = () => [1, 2, 3];
const fs = require('fs');
fs.readFile('eslintlog.json', 'utf8', (err, data) => {
if (err) throw err;
let parsed = JSON.parse(data);
grabWarnings(parsed)
.forEach(generateCmd);
});