Skip to content

Instantly share code, notes, and snippets.

@joeytwiddle
joeytwiddle / debugTools.js
Created November 12, 2017 08:10
Node Debugging Tools (a work in progress)
"use strict";
const realConsole = console;
var debugTools = {
defaultOptions: {
enhanceConsoleLog: false,
listenForUnhandledRejections: true,
Main> go
Rules:
A + B = C -> B = C - A
square(A) = B -> A = sqrt(B)
A + B = C -> A = C - B
A = B -> B = A
Current expression: square(x) + square(y) = square(z)
Want expression of form: x = F
@joeytwiddle
joeytwiddle / top-level-async-function.js
Last active August 8, 2023 09:30
How do you write a top-level async function?
/*
* TLDR: I want to use await in a function, but I don't want my function to return a Promise.
*
* Solution: Use this inside the body of your function: Promise.resolve(async () => { ... }).catch(...);
*
* Original post follows. (I say original, but it has slowly grown longer and longer...!)
*
* ------------------
*
* I have been using async-await and loving it. But there is one thing I'm not sure how to handle.
@joeytwiddle
joeytwiddle / promiseUtils.js
Last active September 27, 2017 11:59
Process a long list of promises in parallel, but with a concurrency limit
/**
* Repeatedly calls doNext() while conditionFn returns true or something truthy.
* Runs maxConcurrent doNext() promises in parallel, starting a new one each time a running one resolves.
*
* Example:
* var toProcess = new Array(2000).fill(true);
* function slowProcess (item) {
* return promiseUtils.delay(100);
* }
@joeytwiddle
joeytwiddle / for_let_while_scoping.js
Last active September 27, 2017 12:06
ES6 scoping with let and for
'use strict'
// For scoping is a bit surprising.
// Although i appears to be a persistent variable between iterations,
// actually there is a separately scoped i for each iteration!
for (let i = 0; i < 5; i++) {
setTimeout(() => console.log(i), 10 * i)
}
// Prints: 0, 1, 2, 3, 4
@joeytwiddle
joeytwiddle / index.html
Created May 4, 2016 08:04 — forked from RickWong/react.html
React without Webpack (using text/template)
<html>
<body>
<div id="react-root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react-with-addons.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react-dom.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.5.2/redux.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.7.7/babel.min.js"></script>
<script id="App" type="text/template">
const App = ({name}) => {
'use strict';
const theGlobal = typeof window === 'object' ? window : global;
const realPromiseConstructor = theGlobal.Promise;
const wrappedPromiseConstructor = function (resolve, reject, progress) {
const originalPromiseInstance = new realPromiseConstructor(resolve, reject, progress);
// Who called us? Let's store it.

How to stop promises from swallowing your errors

TLDR? Follow the Golden Rule of Promises:

If you have a promise, you must either return it, await it, or catch it at the end of the chain.

In the case of a returned promise, it is then the responsibility of the caller to obey the Golden Rule.

Additional: Your response handler and your error handler should be written in such a way that they can never throw errors themselves, or you should follow them with another catch for peace of mind!

// This code is not safe
function renderTemplate(user) {
return '<div>'
+ ' <div>' + user.name + '</div>'
+ ' <div>' + user.tel + '</div>'
+ '</div>';
}
// Why not?
@joeytwiddle
joeytwiddle / gist:fa05e497a09b073250b9
Created November 28, 2014 20:00
Sunride's console.log for Node adds date, file name, line number and function name to log lines
// It may be inefficient, and a little fragile (e.g. if Node changes its stacktrace
// formatting), but it can be a nice tool for developers when it works.
// ### sunlogger.js
var Logger = require('devnull');
var logger = new Logger({namespacing : 0});
var fs = require("fs");