From e5684a1c3ac8635f658fbe4b5ae8f0bb071216c2 Mon Sep 17 00:00:00 2001 From: Petros Aggelatos Date: Mon, 23 Dec 2013 04:22:54 +0000 Subject: [PATCH] Promisify blink --- src/api.coffee | 14 ++++++++------ src/utils.coffee | 10 ++++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/api.coffee b/src/api.coffee index 2177ada8..50d813fd 100644 --- a/src/api.coffee +++ b/src/api.coffee @@ -1,15 +1,17 @@ -fs = require 'fs' +Promise = require 'bluebird' +fs = Promise.promisifyAll require 'fs' +utils = require './utils' express = require 'express' dockerode = require 'dockerode' api = express() LED_FILE = '/sys/class/leds/led0/brightness' -blink = (ms = 200, callback) -> - fs.writeFileSync(LED_FILE, 1) - setTimeout(-> - fs.writeFile(LED_FILE, 0, callback) - , ms) + +blink = (ms = 200) -> + fs.writeFileAsync(LED_FILE, 1) + .then(-> utils.delay(ms)) + .then(-> fs.writeFileAsync(LED_FILE, 0)) api.post('/blink', (req, res) -> interval = setInterval(blink, 400) diff --git a/src/utils.coffee b/src/utils.coffee index aa389d51..98704f14 100644 --- a/src/utils.coffee +++ b/src/utils.coffee @@ -1,7 +1,7 @@ -Promise = require('bluebird') -fs = Promise.promisifyAll(require('fs')) -os = require('os') -crypto = require('crypto') +Promise = require 'bluebird' +fs = Promise.promisifyAll require 'fs' +os = require 'os' +crypto = require 'crypto' # Parses the output of /proc/cpuinfo to find the "Serial : 710abf21" line # or the hostname if there isn't a serial number (when run in dev mode) @@ -16,3 +16,5 @@ exports.getDeviceUuid = -> return crypto.createHash('sha1').update(serial, 'utf8').digest('hex') ) + +exports.delay = (ms) -> new Promise (v) -> setTimeout(v, ms)