Skip to content

Instantly share code, notes, and snippets.

View yamikuronue's full-sized avatar

Bay yamikuronue

View GitHub Profile
@yamikuronue
yamikuronue / Using Async.JS
Created March 19, 2015 18:57
Comparison of callback hell from freezing-octo-cyril
define([
"intern!object",
"intern/chai!assert",
"intern/dojo/node!../../../src/dao/todoItems",
"intern/dojo/node!fs",
"intern/dojo/node!is-there",
"intern/dojo/node!sinon",
"intern/dojo/node!sqlite3",
"intern/dojo/node!async"
], function (registerSuite, assert, dao, fs, IsThere, sinon, sqlite3, async) {
//Controller module
var express = require('express');
var router = express.Router();
app.route('/book')
.get(function(req, res) {
res.send('Get a random book');
})
.post(function(req, res) {
res.send('Add a book');
let data = [{
ID: '1',
Name: 'test game',
Adult: false,
GameMasters: null,
Tags: [],
IC: null
},
{
ID: '2',
dao.getAllGames().then((data) => {
for (let i = 0; i < data.length; i++) {
data[i].canonical = `/api/game/${data[i].id}`;
}
res.send(data);
}).catch((err) => {
//TODO: logging errors
res.status(500).send({ error: 'Database error.' });
});
getAllGames: (res) => {
//For this sprint, all users can see all games
return new Promise((resolve, reject) => {
dao.getAllGames().then((data) => {
for (let i = 0; i < data.length; i++) {
data[i].canonical = `/api/game/${data[i].id}`;
}
res.send(data);
resolve();
getGame: (req, res) => {
if (!req.params.id) {
res.status(501).send({error: 'Missing ID'});
return;
}
return dao.getGame(req.params.id).then((data) => {
if (Array.isArray(data)) {
data = data[0]; //Only the first game
}
//The Rules:
[07:22:05 PM] <yamikuronue> T directs everyone to sit on the bleachers; once Dog drags Li back into the room, T moves to the front, clearing her throat. "Right. So. Most of you lot aren't very impressive physically -- there's a few outliers, but typically, your best chance for survival against someone who outclasses you is to hide what you are. For today's lesson, I've asked a friend to lend a hand. I've told him nothing about you; each of you are
[07:22:05 PM] <yamikuronue> going to take a turn having a chat with him. After five minutes, if he guesses your power type reasonably accurately, he gets a point. If he gets it exactly, he gets two points. If he can't guess, the students get a point. If he guesses wrong, students get two points." She wheels over a chalkboard, writing up "Home" and "Away" on the board to keep track. "Understand?"
//The Cast:
//O: Faerie, the guesser
//Gregor: Wizard, the previous victim
//Li: Native-american Coyote Shapeshifter.
//Set up middleware
const bodyParser = require('body-parser');
let jsonParser = bodyParser.json();
//Attach to route
app.route('/api/boards')
.get(cApi.getAllBoards)
.post(jsonParser, cApi.addBoard)
.patch((_, res) => res.status(405).end())
.delete((_, res) => res.status(405).end())
function getBoard(req, res) {
if (!req.params.id) {
res.status(501).send({error: 'Missing ID'});
return Promise.resolve();
}
return dao.getBoard(req.params.id).then((data) => {
if (Array.isArray(data)) {
data = data[0]; //Only the first board
}
const req = http.request({
host: 'localhost',
port: '8080',
path: '/api/game/1111',
method: 'PUT'
});
req.write(JSON.stringify(formData) + "\n");
req.end();