123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- (function(define) {
- define(function(require) {
- var when = require('./when');
- var Promise = when.Promise;
- var _liftAll = require('./lib/liftAll');
- var slice = Array.prototype.slice;
- var makeApply = require('./lib/apply');
- var _apply = makeApply(Promise, dispatch);
- return {
- lift: lift,
- liftAll: liftAll,
- apply: apply,
- call: call,
- promisify: promisify
- };
-
- function apply(asyncFunction, extraAsyncArgs) {
- return _apply(asyncFunction, this, extraAsyncArgs || []);
- }
-
- function dispatch(f, thisArg, args, h) {
- args.push(alwaysUnary(h.resolve, h), alwaysUnary(h.reject, h));
- tryCatchResolve(f, thisArg, args, h);
- }
- function tryCatchResolve(f, thisArg, args, resolver) {
- try {
- f.apply(thisArg, args);
- } catch(e) {
- resolver.reject(e);
- }
- }
-
- function call(asyncFunction/*, arg1, arg2...*/) {
- return _apply(asyncFunction, this, slice.call(arguments, 1));
- }
-
- function lift(f/*, args...*/) {
- var args = arguments.length > 1 ? slice.call(arguments, 1) : [];
- return function() {
- return _apply(f, this, args.concat(slice.call(arguments)));
- };
- }
-
- function liftAll(src, combine, dst) {
- return _liftAll(lift, combine, dst, src);
- }
-
- function promisify(asyncFunction, positions) {
- return function() {
- var thisArg = this;
- return Promise.all(arguments).then(function(args) {
- var p = Promise._defer();
- var callbackPos, errbackPos;
- if(typeof positions.callback === 'number') {
- callbackPos = normalizePosition(args, positions.callback);
- }
- if(typeof positions.errback === 'number') {
- errbackPos = normalizePosition(args, positions.errback);
- }
- if(errbackPos < callbackPos) {
- insertCallback(args, errbackPos, p._handler.reject, p._handler);
- insertCallback(args, callbackPos, p._handler.resolve, p._handler);
- } else {
- insertCallback(args, callbackPos, p._handler.resolve, p._handler);
- insertCallback(args, errbackPos, p._handler.reject, p._handler);
- }
- asyncFunction.apply(thisArg, args);
- return p;
- });
- };
- }
- function normalizePosition(args, pos) {
- return pos < 0 ? (args.length + pos + 2) : pos;
- }
- function insertCallback(args, pos, callback, thisArg) {
- if(typeof pos === 'number') {
- args.splice(pos, 0, alwaysUnary(callback, thisArg));
- }
- }
- function alwaysUnary(fn, thisArg) {
- return function() {
- if (arguments.length > 1) {
- fn.call(thisArg, slice.call(arguments));
- } else {
- fn.apply(thisArg, arguments);
- }
- };
- }
- });
- })(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); });
|