Skip to content

Instantly share code, notes, and snippets.

View benjamingr's full-sized avatar
🖊️
Limited availability.

Benjamin Gruenbaum benjamingr

🖊️
Limited availability.
View GitHub Profile
@benjamingr
benjamingr / promise.js
Created January 19, 2015 21:10
Got bored without internet
let once = (fns, called) => // fns is an array of functions, called is undefined if first call
fns.map(orig => function OnceBinded(){
if(called) return; else called = true;
return orig(...arguments);
});
export class Promise {
constructor(resolver){
Object.assign(this, {_handlers: [] , _state : State.Pending, _value: null});
resolver(value => {
if(this._state !== State.Pending) return;
@benjamingr
benjamingr / gist:0237932cee84712951a2
Last active October 6, 2023 08:31
Promise unhandled rejection tracking global handler hook

Possibly Unhandled Rejection NodeJS Promise Hook

###Unhandled Rejection Tracking

Several promise libraries such as bluebird and when as well as some native promise implementations offer potentially unhandled rejection tracking. This means that the following:

Promise.reject(new Error("err")); // never attach a `catch`
@benjamingr
benjamingr / Promise.swift
Created August 13, 2014 08:32
promise library in swift
//
// Promise.swift
// Promise
//
// Created by Benjamin Gruenbaum on 8/12/14.
// Copyright (c) 2014 Tipranks. All rights reserved.
//
import Foundation
//
// Promise.swift
// Promise
//
// Created by Benjamin Gruenbaum on 8/12/14.
// Copyright (c) 2014 Tipranks. All rights reserved.
//
import Foundation
func then<NT,NE>(onFulfilled:(T) -> Promise<NT,NE>) -> Promise<NT,NE> {
var p = Promise<NT,NE>()
switch self.state.state {
case .Rejected:
p.reject(self.state.error) // this is a type error
case .Fulfilled:
let result = onFulfilled(self.state.value!)
result.then( { p.fulfill($0) })
result.catch( {p.reject($0) })
case .Pending:
@benjamingr
benjamingr / gist:5b42be523b7f313a79b7
Created August 12, 2014 09:10
lol, promise constructor
//
// Promise.swift
// Promise
//
// Created by Benjamin Gruenbaum on 8/12/14.
// Copyright (c) 2014 Tipranks. All rights reserved.
//
import Foundation
//
// Promise.swift
// Promise
//
// Created by Benjamin Gruenbaum on 8/12/14.
// Copyright (c) 2014 Tipranks. All rights reserved.
//
import Foundation
class Foo<A,B>{
var bar:[(A,B)] = [(A,B)]() // why is this invalid?
}
@benjamingr
benjamingr / gist:0ed038727f38fb77e7ee
Created May 20, 2014 23:10
Resolve a dependency recursively based on .needs
// this will load a script, I assume each dependency contains what it needs inside
// `dependency.needs` and we'll load that.
//
// stuff is called script here, but it could also be a stylesheet or whatever.
//
// Since when we return a promise from a `.then` it unwraps and runs the promise we return, we can
// return a promise for the values of our _own_ dependencies, and continue doing so, promises will
// unwrap everything for us.
//
// This does not do have the same `.addDependency` interface from before, it's just a function to show
function ResourceBuilder(){
this._dependencies = [];
}
ResourceBuilder.prototype.addDependency = function(dep){
this._dependencies.add(dep);
}
ResourceBuilder.protototype.get = function(){
var loadPromises = this._dependencies.map(loadDependency); // alternatively, dependencies could be promises already
return Promise.all(loadPromises).then(function(loadedResourcesArray){