ADAMKDEAN

ES6 compatible sleep function

Published Tuesday, 24 March 2015

While debugging locally, it can be hard to see how an application runs in the wild as the network has no delay. Quite often, you get around this using a sleep function.

Pre-ES6 generators, you might do this with a callback, maybe using a setTimeout that calls the callback.

function sleep(ms, callback) {
    setTimeout(callback, ms);
}

With ES6 generators, where you want to yield sleep(1000) etc, you can't use callbacks. What you can do is return a function that takes a single parameter, done, which through closure has access to the parameter you want to pass in, ms. When the returned function is called by whatever cog under the hood calls the returned functions when yielding, your inner function will have access to the ms parameter you passed in, along with a callback that JS passes in, which when called, will continue on from where you yielded the sleep.

function sleep(ms) {
    return function(done) {
        setTimeout(done, ms);
    }
}

yield sleep(1000);

This is now available on npmjs.org. To install:

npm install es6-sleep

To use, let's say while inside some Koa middleware:

var sleep = require('es6-sleep');

app.use(function *() {
    // do something
    yield sleep(1000);
    // continue
});

Hopefully that makes sense. It does in my head.

Intercepting XMLHttpRequest requests

Published Wednesday, 11 March 2015

Let's kick off the continuation of my code blogging with a very helpful little snippet. I'm just removing this from a codebase so it can sit here for eternity instead.

(function(open) {
    XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
        // do some magic
        open.call(this, method, url, async, user, pass);
    };
})(XMLHttpRequest.prototype.open);

This will intercept the request before it happens, and once you've done whatever changes you need to do, be it logging or tagging, you can continue it.

New website is now live

Published Saturday, 21 February 2015

Finally, the new website is now live. There are still some features to complete, such as searching, commenting, responsiveness, blog archive and more, but we're close enough to go live.

Since the fateful event of August 2014 when my darling girlfriend turned off all the power before a holiday and somehow destroyed the previous server and therein the old website, this site had been kept running barely by Ghost.

Well, that's a thing of the past now. Ghost is...well, a ghost. A codebase arises.

I'll be actively adding features and finishing off the edges over the next few months.

Oh, and as a bonus, backlinks from my old website now work too.

Disable iptables

Published Monday, 8 December 2014

Rather than disabling iptables in Ubuntu 14.04, you can allow everything.

iptables -F
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

It's bad, but it works.

 
Showing posts 1-5 of 154