UNPKG

3.84 kBJavaScriptView Raw
1/** @license MIT License (c) copyright 2013-2014 original author or authors */
2
3/**
4 * Collection of helper functions for wrapping and executing 'traditional'
5 * synchronous functions in a promise interface.
6 *
7 * @author Brian Cavalier
8 * @contributor Renato Zannon
9 */
10
11(function(define) {
12define(function(require) {
13
14 var when = require('./when');
15 var attempt = when['try'];
16 var _liftAll = require('./lib/liftAll');
17 var _apply = require('./lib/apply')(when.Promise);
18 var slice = Array.prototype.slice;
19
20 return {
21 lift: lift,
22 liftAll: liftAll,
23 call: attempt,
24 apply: apply,
25 compose: compose
26 };
27
28 /**
29 * Takes a function and an optional array of arguments (that might be promises),
30 * and calls the function. The return value is a promise whose resolution
31 * depends on the value returned by the function.
32 * @param {function} f function to be called
33 * @param {Array} [args] array of arguments to func
34 * @returns {Promise} promise for the return value of func
35 */
36 function apply(f, args) {
37 // slice args just in case the caller passed an Arguments instance
38 return _apply(f, this, args == null ? [] : slice.call(args));
39 }
40
41 /**
42 * Takes a 'regular' function and returns a version of that function that
43 * returns a promise instead of a plain value, and handles thrown errors by
44 * returning a rejected promise. Also accepts a list of arguments to be
45 * prepended to the new function, as does Function.prototype.bind.
46 *
47 * The resulting function is promise-aware, in the sense that it accepts
48 * promise arguments, and waits for their resolution.
49 * @param {Function} f function to be bound
50 * @param {...*} [args] arguments to be prepended for the new function @deprecated
51 * @returns {Function} a promise-returning function
52 */
53 function lift(f /*, args... */) {
54 var args = arguments.length > 1 ? slice.call(arguments, 1) : [];
55 return function() {
56 return _apply(f, this, args.concat(slice.call(arguments)));
57 };
58 }
59
60 /**
61 * Lift all the functions/methods on src
62 * @param {object|function} src source whose functions will be lifted
63 * @param {function?} combine optional function for customizing the lifting
64 * process. It is passed dst, the lifted function, and the property name of
65 * the original function on src.
66 * @param {(object|function)?} dst option destination host onto which to place lifted
67 * functions. If not provided, liftAll returns a new object.
68 * @returns {*} If dst is provided, returns dst with lifted functions as
69 * properties. If dst not provided, returns a new object with lifted functions.
70 */
71 function liftAll(src, combine, dst) {
72 return _liftAll(lift, combine, dst, src);
73 }
74
75 /**
76 * Composes multiple functions by piping their return values. It is
77 * transparent to whether the functions return 'regular' values or promises:
78 * the piped argument is always a resolved value. If one of the functions
79 * throws or returns a rejected promise, the composed promise will be also
80 * rejected.
81 *
82 * The arguments (or promises to arguments) given to the returned function (if
83 * any), are passed directly to the first function on the 'pipeline'.
84 * @param {Function} f the function to which the arguments will be passed
85 * @param {...Function} [funcs] functions that will be composed, in order
86 * @returns {Function} a promise-returning composition of the functions
87 */
88 function compose(f /*, funcs... */) {
89 var funcs = slice.call(arguments, 1);
90
91 return function() {
92 var thisArg = this;
93 var args = slice.call(arguments);
94 var firstPromise = attempt.apply(thisArg, [f].concat(args));
95
96 return when.reduce(funcs, function(arg, func) {
97 return func.call(thisArg, arg);
98 }, firstPromise);
99 };
100 }
101});
102})(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); });
103
104