Skip to content

Instantly share code, notes, and snippets.

View aluanhaddad's full-sized avatar

Aluan Haddad aluanhaddad

View GitHub Profile
@domenic
domenic / promises.md
Last active March 31, 2024 14:07
You're Missing the Point of Promises

This article has been given a more permanent home on my blog. Also, since it was first written, the development of the Promises/A+ specification has made the original emphasis on Promises/A seem somewhat outdated.

You're Missing the Point of Promises

Promises are a software abstraction that makes working with asynchronous operations much more pleasant. In the most basic definition, your code will move from continuation-passing style:

getTweetsFor("domenic", function (err, results) {
 // the rest of your code goes here.

Parens And Performance

Years ago, some smart folks that worked on JS engines realized that not all JS that's loaded into a page/app initially is needed right away. They implemented JIT to optimize this situation.

JIT means Just-In-Time, which means essentially that the engine can defer processing (parsing, compiling) certain parts of a JS program until a later time, for example when the function in question is actually needed. This deferral means the engine is freer to spend the important cycles right now on the code that's going to run right now. This is a really good thing for JS performance.

Some time later, some JS engine devs realized that they needed to get some hints from the code as to which functions would run right away, and which ones wouldn't. In technical speak, these hints are called heuristics.

So they realized that one very common pattern for knowing that a function was going to run right away is if the first character before the function keyword was a (, because that usually m

@robertleeplummerjr
robertleeplummerjr / dch.ts
Created October 28, 2017 22:13
Dynamic Class Hierarchy in Typescript
class Item {
public static async get<Options extends { id: string }>(o: Options, api: Api): Promise<Item> {
return new Promise<Item>((accept, reject) => {
console.log(api);
accept(new Item());
});
}
public static async search<Options extends { query: string }>(o: Options, api: Api): Promise<Item[]> {
return new Promise<Item[]>((accept, reject) => {
@maggiben
maggiben / typeahead.html
Created January 25, 2016 08:40
Aurelia typeahead
<template>
<require from="/styles/type-ahead.css"></require>
<div class="ui icon input">
<input type="text" class="form-control" placeholder.bind="options.placeholder" value.bind="value">
<i class="search link icon"></i>
</div>
</template>