Skip to content

Instantly share code, notes, and snippets.

View ithinkihaveacat's full-sized avatar

Michael Stillwell ithinkihaveacat

View GitHub Profile
function F(fn) {
return function (/* arguments */) {
var args = [this].concat(Array.prototype.slice.call(arguments));
return new Promise(function (resolve, reject) {
Promise.all(args).then(function (/* arguments */) {
try {
Promise.resolve(
fn.apply(arguments[0].shift(), arguments[0])
).then(resolve, reject);
} catch (e) {

Comparison of two different approaches to structuring code that uses Promises.

Version using .then(), from Complex task dependencies:

files.getLastTwoVersions(filename)
    .then(function(items) {
        return [versions.get(items.last), 
                versions.get(items.previous)];
 })

Compared to the .then() style, this approach:

  • Looks almost identical to the non-Promise version--you end up with imperative code, just like the non-Promise equivalent, instead of long .then() chains. (Unfortunately you do still need to use .catch() to handle exceptions/rejections.)
  • Supports functions of multiple arguments, including functions where some of the arguments are Promises, and some not.
  • Works by transforming a normal-looking function--if you have a pure function that is not asynchronous, with primitive types as arguments and a primitive type as a return value, you don't even need to know that Promises are involved.
  • With a helper function, it also supports fairly natural-looking method calls where the arguments and return v
#!/bin/bash
# NOTE: This (sadly) doesn't work in all situations. See
# https://github.com/composer/composer/pull/1083
# for discussion of some alternatives.
# Checks whether composer.json has changed after checkout, in which
# case you probably need to run 'composer install'.
#
# INSTALLATION
<?php
array (
0 =>
array (
'file' => '/usr/share/php/BBC/Controller/Plugin/CacheControl.php',
'line' => 59,
'function' => 'rawstack',
),
1 =>
<?php
/**
* Similar to print_r(), except that it writes to /tmp/rawlog.log. This function is
* intended for debugging, where you want to be absolutely sure that your output
* is not being captured or buffered or otherwise interfered with.
*
* One good way to ensure this is always available is to use the auto_prepend_file
* php.ini setting.
*/
@ithinkihaveacat
ithinkihaveacat / child-process-event-helper.js
Created February 15, 2013 23:15
Creates event-listener-style communication between parent and child.
function onChild(child, event, callback) {
child.on('message', (function (args) {
if (args[0] === event) {
callback.apply(null, Array.prototype.slice.call(args, 1));
}
}));
}
function emitChild(/* child, event, [arg1], [arg2], [...] */) {
var args = Array.prototype.slice.call(arguments);
@ithinkihaveacat
ithinkihaveacat / Vagrantfile.rb
Created September 28, 2012 10:28
Vagrantfile snippet to check that apache_server_name resolves to vm_ip
# Vagrantfile snippet to check that apache_server_name resolves to vm_ip (i.e.
# assuming that the rest of the Vagrantfile configures a VM in this way).
apache_server_name = "myproject.local"
vm_ip = "192.168.33.111"
begin
res = Socket::getaddrinfo(apache_server_name, nil, Socket::AF_INET)
rescue SocketError => e
# s doesn't resolve to anything at all
mycallback({
"count": 10,
"value": {
"title": "test",
"description": "Pipes Output",
"link": "http://pipes.yahoo.com/pipes/pipe.info?_id=bc115235e32da3e0f6db46cd7a159a01",
"pubDate": "Sun, 12 Aug 2012 21:00:39 +0000",
"generator": "http://pipes.yahoo.com/pipes/",
"callback": "mycallback",
"items": [