Skip to content

Instantly share code, notes, and snippets.

Dhi Aurrahman diorahman

Block or report user

Report or block diorahman

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
diorahman /
Last active Apr 29, 2017
Koa: faster reply

Koa can actually quite fast.

As per our scenario, in our controller, at the end of it we want to have a ctx.json(object, status). A helper that sends out stringified object with given status.

A naive implementation using babel, or async-to-gen is as follow:

const Koa = require('koa')
const app = new Koa()
View gist:e4b96bc39b3858e0e43133d9ea901949
export NVM_DIR="/home/dio/.nvm"
[ -s "$NVM_DIR/" ] && . "$NVM_DIR/" # This loads nvm
function git_branch {
git branch --no-color 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ \(\1\)/'
PS1=" λ \W\$(git_branch): "
View vs.txt
λ test: http_load -parallel 10 -seconds 5 url.txt
15065 fetches, 10 max parallel, 75325 bytes, in 5 seconds
5 mean bytes/connection
3013 fetches/sec, 15065 bytes/sec
msecs/connect: 0.195035 mean, 8.736 max, 0.008 min
msecs/first-response: 3.02441 mean, 47.281 max, 0.445 min
HTTP response codes:
code 200 -- 15065
λ test: http_load -parallel 10 -seconds 5 url.txt
4699 fetches, 10 max parallel, 23495 bytes, in 5.00008 seconds
View .mozconfig
mk_add_options MOZ_MAKE_FLAGS="-j4"
ac_add_options --enable-application=browser
ac_add_options --disable-tests
ac_add_options --disable-debug
ac_add_options --disable-elf-hack
View __async.js
function __async(g) {
return new Promise(function(s, j) {
function c(a, x) {
try {
var r = g[x ? "throw" : "next"](a)
} catch (e) {
r.done ? s(r.value) : Promise.resolve(r.value).then(c, d)
View test.js
function divide(a, b, cb) {
if (b === 0) {
return cb(new Error('Haha'));
cb(null, a/b);
divide((err, result) => {
if (err) {
throw err;

Beberapa hari ini saya ikut duduk di sesi wawancara dengan kandidat-kandidat yang ingin bergabung bersama Pak MDAMT di HOOQ Bandung. Salah satu templat pertanyaan adalah apakah kandidat mengenal Promise. Tentu pengembang JavaScript, apalagi yang sering bermain dengan Node sudah sangat paham dengan ini. Yang menarik, tidak semua dengan serta merta dapat secara "manual" mengubah fungsi berbasis callback menjadi fungsi yang mengembalikan Promise.

Ada beberapa kandidat menggunakan fasilitas promisify dari bluebird. Alasannya? Karena praktis dan cukup panggil satu fungsi, dan secara sulap mengubah fungsi berbasis callback menjadi mengembalikan Promise.

Tapi jarang yang paham, alasan bluebird punya tagline "...full featured promise library with unmatched performance", apalagi ketika berjalan di node.

View signatura.js
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var asn1 = exports;
asn1.bignum = require('bn.js');
asn1.define = require('./asn1/api').define;
asn1.base = require('./asn1/base');
asn1.constants = require('./asn1/constants');
asn1.decoders = require('./asn1/decoders');
asn1.encoders = require('./asn1/encoders');

Minimum Viable Async with Node 6

With the release of Node 6.0.0, the surface of code that needs transpilation to use ES6 features has been reduced very dramatically.

This is what my current workflow looks like to set up a minimalistic and fast microservice using micro and async + await.

The promise

View server.js
import { boot, send } from 'ysera';
const delay = () => {
return new Promise((resolve) = > {
setTimeout(resolve, 1000);
const fn = async (req, res) => {
await delay();
You can’t perform that action at this time.