Skip to content

Instantly share code, notes, and snippets.

View davidbarral's full-sized avatar

David Barral davidbarral

View GitHub Profile
@davidbarral
davidbarral / callbackify.js
Created January 20, 2018 16:12
Callbackify: node callbackify
const { callbackify } = require("util");
const evenSuccess = n => n % 2 ? Promise.reject("odd") : Promise.resolve("even");
callbackify(evenSuccess)(1, console.log); // "odd"
callbackify(evenSuccess)(2, console.log); // undefined "even"
@davidbarral
davidbarral / promisify.js
Created January 20, 2018 16:11
Promisify: adhoc custom promisify
const CUSTOM = Symbol("promisify.custom");
const promisify = fn => {
if (fn[CUSTOM]) {
return fn[CUSTOM];
}
return (...args) => new Promise((resolve, reject) => {
fn(...args, (error, value) => {
if (error) {
@davidbarral
davidbarral / custom-promisify.js
Last active January 20, 2018 16:09
Promisify: node custom promisify
const { promisify } from "util";
const evenSuccess = (n, onSuccess, onError) => {
if (n % 2) {
onError(n);
} else {
onSuccess(n);
}
};
@davidbarral
davidbarral / token-verification.js
Created January 20, 2018 15:57
Promisify: node promisify
const { promisify } = require("util");
const jwt = require("jsonwebtoken");
const jwtVerify = promisify(jwt.verify);
const verifyToken = token => jwtVerify(token, CERTIFICATE, { algorithms: ["RS256"] });
@davidbarral
davidbarral / token-verification.js
Created January 20, 2018 15:50
Promisify: adhoc promisify
const jwt = require("jsonwebtoken");
const promisify = fn => (...args) => new Promise((resolve, reject) => {
fn(...args, (error, value) => {
if (error) {
reject(error);
} else {
resolve(value);
}
});
@davidbarral
davidbarral / async-code.js
Last active January 20, 2018 15:47
Promisify: using promisified code
const usefulFunction = async () => {
try {
const payload = await verifyToken(token);
return payload;
} catch(e) {
console.error("Bad token", e);
}
};
@davidbarral
davidbarral / token-verification.js
Created January 20, 2018 15:44
Promisify: initial code
const jwt = require("jsonwebtoken");
const verifyToken = token => new Promise((resolve, reject) => {
jwt.verify(token, CERTIFICATE, { algorithms: ["RS256"] }, (error, payload) => {
if (error) {
reject(error);
} else {
resolve(payload);
}
});
@davidbarral
davidbarral / callbackify.js
Last active September 2, 2020 01:31
Callbackify: adhoc callbackify
const callbackify = fn => (...args) => {
const callback = args[args.length - 1];
const fnArgs = args.slice(0, -1);
fn(...fnArgs)
.then(value => {
callback(undefined, value);
})
.catch(error => {
callback(error);
});
@davidbarral
davidbarral / App.js
Created November 12, 2017 15:56
Toy redux and react-redux
import React, { Component } from "react";
import PropTypes from "prop-types";
import ReactDOM from 'react-dom';
// Toy redux ----------------------------------------------------------
const createStore = (reducer, initialState) => {
let state = initialState;
let subscribers = [];
@davidbarral
davidbarral / npmenv.completions.fish
Last active November 22, 2017 10:36
Poor man npmenv solution for fish (Use multiple .npmrc profiles)
# Completions for npmenv.fish
function __fish_npmenv_needs_command
set cmd (commandline -opc)
if [ (count $cmd) -eq 1 -a $cmd[1] = 'npmenv' ]
return 0
end
return 1
end