This file contains hidden or 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 { 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" |
This file contains hidden or 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 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) { |
This file contains hidden or 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 { promisify } from "util"; | |
| const evenSuccess = (n, onSuccess, onError) => { | |
| if (n % 2) { | |
| onError(n); | |
| } else { | |
| onSuccess(n); | |
| } | |
| }; |
This file contains hidden or 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 { promisify } = require("util"); | |
| const jwt = require("jsonwebtoken"); | |
| const jwtVerify = promisify(jwt.verify); | |
| const verifyToken = token => jwtVerify(token, CERTIFICATE, { algorithms: ["RS256"] }); |
This file contains hidden or 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 jwt = require("jsonwebtoken"); | |
| const promisify = fn => (...args) => new Promise((resolve, reject) => { | |
| fn(...args, (error, value) => { | |
| if (error) { | |
| reject(error); | |
| } else { | |
| resolve(value); | |
| } | |
| }); |
This file contains hidden or 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 usefulFunction = async () => { | |
| try { | |
| const payload = await verifyToken(token); | |
| return payload; | |
| } catch(e) { | |
| console.error("Bad token", e); | |
| } | |
| }; |
This file contains hidden or 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 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); | |
| } | |
| }); |
This file contains hidden or 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 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); | |
| }); |
This file contains hidden or 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, { Component } from "react"; | |
| import PropTypes from "prop-types"; | |
| import ReactDOM from 'react-dom'; | |
| // Toy redux ---------------------------------------------------------- | |
| const createStore = (reducer, initialState) => { | |
| let state = initialState; | |
| let subscribers = []; |
This file contains hidden or 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
| # 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 |