twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

nodeify.js (1647B)


      1 "use strict";
      2 module.exports = function(Promise) {
      3 var util = require("./util");
      4 var async = Promise._async;
      5 var tryCatch = util.tryCatch;
      6 var errorObj = util.errorObj;
      7 
      8 function spreadAdapter(val, nodeback) {
      9     var promise = this;
     10     if (!util.isArray(val)) return successAdapter.call(promise, val, nodeback);
     11     var ret =
     12         tryCatch(nodeback).apply(promise._boundValue(), [null].concat(val));
     13     if (ret === errorObj) {
     14         async.throwLater(ret.e);
     15     }
     16 }
     17 
     18 function successAdapter(val, nodeback) {
     19     var promise = this;
     20     var receiver = promise._boundValue();
     21     var ret = val === undefined
     22         ? tryCatch(nodeback).call(receiver, null)
     23         : tryCatch(nodeback).call(receiver, null, val);
     24     if (ret === errorObj) {
     25         async.throwLater(ret.e);
     26     }
     27 }
     28 function errorAdapter(reason, nodeback) {
     29     var promise = this;
     30     if (!reason) {
     31         var newReason = new Error(reason + "");
     32         newReason.cause = reason;
     33         reason = newReason;
     34     }
     35     var ret = tryCatch(nodeback).call(promise._boundValue(), reason);
     36     if (ret === errorObj) {
     37         async.throwLater(ret.e);
     38     }
     39 }
     40 
     41 Promise.prototype.asCallback = Promise.prototype.nodeify = function (nodeback,
     42                                                                      options) {
     43     if (typeof nodeback == "function") {
     44         var adapter = successAdapter;
     45         if (options !== undefined && Object(options).spread) {
     46             adapter = spreadAdapter;
     47         }
     48         this._then(
     49             adapter,
     50             errorAdapter,
     51             undefined,
     52             this,
     53             nodeback
     54         );
     55     }
     56     return this;
     57 };
     58 };