deferred化しました
使い方は次のようになります
dLevel = require "dLevel"
db = dLevel("./mydb")
db.set("name", "dLevel")
.then(()->
db.get("name")
)
.then((val)->
console.log(val)
db.end()
)
"use strict" | |
deferred = require "deferred" | |
Level = require "level" | |
module.exports = dLevel = (dbname)-> | |
dLevel = {} | |
db = Level dbname | |
for k, v of db | |
continue unless typeof v is 'function' | |
do (k, v)-> | |
dLevel[k] = (args...)-> | |
d = deferred() | |
args.push (err, ret...)-> | |
return d.reject err if err | |
d.resolve.apply d, ret | |
db[k].apply db, args | |
d.promise | |
dLevel | |
"use strict" | |
deferred = require "deferred" | |
MySql = require "mysql" | |
dMySql = (config = {})-> | |
mysql = MySql.createConnection(config) | |
dMySql = {} | |
for k, v of mysql | |
continue unless typeof v is 'function' | |
do (k, v)-> | |
dMySql[k] = (args...)-> | |
d = deferred() | |
args.push (err, ret...)-> | |
return d.reject err if err | |
d.resolve.apply d, ret | |
mysql[k].apply mysql, args | |
d.promise | |
# end does not require callback | |
dMySql.end = ()-> | |
d = deferred() | |
d.resolve mysql.end() | |
d.promise | |
dMySql |
"use strict" | |
deferred = require "deferred" | |
Redis = require "redis" | |
dRedis = (port = 6379, host = "localhost", options)-> | |
redis = Redis.createClient(port, host, options) | |
dRedis = {} | |
for k, v of redis | |
continue unless typeof v is 'function' | |
do (k, v)-> | |
dRedis[k] = (args...)-> | |
d = deferred() | |
args.push (err, ret...)-> | |
return d.reject err if err | |
d.resolve.apply d, ret | |
redis[k].apply redis, args | |
d.promise | |
# end does not require callback | |
dRedis.end = ()-> | |
d = deferred() | |
d.resolve redis.end() | |
d.promise | |
dRedis |