'use strict';

var cc = require('./core');


var ncallback = function(deferred) {
  return function(err, val) {
    if (err)
      deferred.reject(new Error(err));
    else
      deferred.resolve(val);
  };
};


var nbind = function(fn, context) {
  var boundArgs = Array.prototype.slice.call(arguments, 2);

  return function() {
    var args = Array.prototype.slice.call(arguments);
    var result = cc.defer();

    fn.apply(context, boundArgs.concat(args, ncallback(result)));

    return result;
  };
};


var nodeify = function(deferred, callback) {
  if (callback === undefined)
    return deferred;
  else {
    deferred.then(function(val) {
      callback(null, val)
    }, function(err) {
      callback(err);
    });
  }
};


module.exports = {
  ncallback: ncallback,
  nbind    : nbind,
  nodeify  : nodeify
};

@@ -34,24 +34,187 @@
 ./core');%0A%0A%0A
+var sleep = function(ms) %7B%0A  var result = cc.defer();%0A  var t = setTimeout(function() %7B%0A    clearTimeout(t);%0A    result.resolve();%0A  %7D, ms);%0A  return result;%0A%7D;%0A%0A%0A
 var ncallbac
@@ -912,16 +912,36 @@
 rts = %7B%0A
+  sleep    : sleep,%0A
   ncallb

move sleep() from ceci-channels to ceci-core