Skip to content

Instantly share code, notes, and snippets.

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!

@joeytwiddle
joeytwiddle / gist:6129676
Last active December 20, 2022 15:25
Deep population helper for mongoose
// Example usage:
// deepPopulate(blogPost, "comments comments._creator comments._creator.blogposts", {sort:{title:-1}}, callback);
// Note that the options get passed at *every* level!
// Also note that you must populate the shallower documents before the deeper ones.
function deepPopulate(doc, pathListString, options, callback) {
var listOfPathsToPopulate = pathListString.split(" ");
function doNext() {
if (listOfPathsToPopulate.length == 0) {
// Now all the things underneath the original doc should be populated. Thanks mongoose!
callback(null,doc);
@joeytwiddle
joeytwiddle / 1_small_repos_first_please.md
Last active July 23, 2020 21:08
Me complaining about GitHub notification UI v2

This is what I sent to GitHub's feedback form (plus a few edits)

The new notifications page is blocking me from working on small open source projects!

The first thing I want to do is see the projects with the least new notifications. But you have truncated them off the bottom of the list! (The list down the left-hand side.)

With the old UI, I used to immediately scroll to the bottom, and interact with the small projects first, before reading the notifications of larger projects.

Why? Because these small activity projects are more likely to be projects with only a few contributors, where I can make the most impact. Sometimes they are even my own projects. These are usually the projects of most interest to me.

@joeytwiddle
joeytwiddle / Basic_React_Gotchas.md
Last active May 20, 2020 05:40
Some things we learned in the first few months of using React

Basic React Gotchas

Some things we learned in the first few months of using React (2017).

Note: This document might not be easy for complete beginners. It was originally written for someone who already knew React fairly well, and wanted some suggested talking points for a presentation they were planning to give to beginners. I have added some examples over time, but there may be room for more.

Also note: This document was written before React hooks, when we were using ES6 classes for components. Some of the advice (especially the earlier sections, and some of the error messages) does not really apply when using hooks.

Hooks have a number of new and different gotchas, deserving of their own document!

@joeytwiddle
joeytwiddle / nn-images.html
Last active November 17, 2019 06:51
A neural network to generate images
<html>
<body>
<canvas id="canvas" width="512" height="512"></canvas>
<!-- <script src="./nn-image.js"></script> -->
<script>
class Network {
layers = []
weightsLayers = []
@joeytwiddle
joeytwiddle / optional.js
Last active November 12, 2019 08:14
Getting properties from an object which might be null
// A common situation in Javascript:
//
// We have a variable `obj`, and we want to get `obj.foo.bar`
//
// But `obj` might be null or undefined, or `obj.foo` might be null or undefined
//
// So how can we try to get `obj.foo.bar` without risking an error?
// Here are a few different ways to do that. Which one is clearest?
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const fn = (ms) => delay(ms).then(() => console.log('done after ' + ms));
// Sequential
async function test1() {
const [one, two] = [await fn(2000), await fn(1000)];
console.log('mikael1 finished');
}
@joeytwiddle
joeytwiddle / asyncWrappersForRestify.js
Last active October 7, 2019 02:57
Helper to use promises in restify (might also work for express)
/**
* Use restify with promises
*
* Restify middleware and route handlers are passed a callback function `next`
* which must be called on success, or on an error.
*
* But you may prefer to define your middleware and routes using promises or
* async functions instead of using callbacks.
*
* To achieve that, simply wrap your middleware functions in `middleware()` and
@joeytwiddle
joeytwiddle / move_dropbox_to_ext4_fs.sh
Created November 6, 2018 05:07
Creates a file with an ext4 filesystem inside it, and moves Dropbox into that filesystem
#!/bin/bash
set -e
set -x
# You could put the fs file and the mountpoint inside your home folder
#new_fs_file="$HOME/dropbox_partition.ext4fs"
#mountpoint="$HOME/Dropbox.mnt"
# But that may not work if you are using a fuse-mounted homefolder (e.g. encrypted with encfs)
new_fs_file="/home/dropbox_for_$USER.ext4fs"
mountpoint="/mnt/dropbox.$USER"
@joeytwiddle
joeytwiddle / cooldown-monitor.js
Created April 30, 2019 04:40
Cooldown Monitor
"use strict";
/*
Simulates an object which can heat up and overheat, but with a constant rate of cooling over time.
Useful for throttling when you want to provide an initial allowance.
For example, it could be used to manage the overheating of a weapon in a game, or to avoid errors from generating huge logfiles, but without throttling the first few errors.