Last active
April 27, 2016 02:49
-
-
Save JacksonGariety/8cbf0b655847b32fdd82a7240a58c735 to your computer and use it in GitHub Desktop.
A koa server that always responds with the next number in a fibonacci sequence and stops after the 100th number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'babel-polyfill' | |
import Koa from 'koa' | |
const app = new Koa(); | |
function* fibonacci () { | |
let [a, b] = [0, 1] | |
while (true) { | |
[a, b] = [b, a + b] | |
yield a | |
} | |
} | |
function* take(n, iterator) { | |
for (let item of iterator) { | |
if (n-- === 0) break | |
yield item | |
} | |
} | |
const seq = take(100, fibonacci()) | |
app.use(async (ctx, next) => { | |
var start = new Date; | |
await next(); | |
var ms = new Date - start; | |
console.log('%s %s - %sms', ctx.method, ctx.url, ms); | |
}) | |
app.use(async ctx => { | |
ctx.body = seq.next().value | |
}); | |
app.listen(1337) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment