Debian Codegraph @

deferred/ext/promise/invoke-async.js

raw
1 /* eslint max-statements: "off" */
2
3 // 'invokeAsync' - Promise extension
4 //
5 // promise.invokeAsync(name[, arg0[, arg1[, ...]]])
6 //
7 // On resolved object calls asynchronous method that takes callback
8 // (Node.js style).
9 // Do not pass callback, it's handled by internal implementation.
10 // 'name' can be method name or method itself.
11
12 "use strict";
13
14 var toArray = require("es5-ext/array/to-array")
15 , isValue = require("es5-ext/object/is-value")
16 , isCallable = require("es5-ext/object/is-callable")
17 , deferred = require("../../deferred")
18 , isPromise = require("../../is-promise")
19 , processArguments = require("../_process-arguments");
20
21 var slice = Array.prototype.slice
22 , apply = Function.prototype.apply
23 , reject = deferred.reject
24 , applyFn;
25
26 applyFn = function (fn, args, localResolve, localReject) {
27 var result;
28 if (fn.returnsPromise) {
29 try {
30 result = apply.call(fn, this, args);
31 } catch (e) {
32 localReject(e);
33 return;
34 }
35 localResolve(result);
36 return;
37 }
38 args = toArray(args).concat(function (error, localResult) {
39 if (isValue(error)) {
40 localReject(error);
41 } else {
42 localResolve(arguments.length > 2 ? slice.call(arguments, 1) : localResult);
43 }
44 });
45 try { apply.call(fn, this, args); }
46 catch (e2) { localReject(e2); }
47 };
48
49 deferred.extend(
50 "invokeAsync",
51 function (methodIgnored/*, …args*/) {
52 var def;
53 if (!this.pending) this.pending = [];
54 def = deferred();
55 this.pending.push("invokeAsync", [arguments, def.resolve, def.reject]);
56 return def.promise;
57 },
58 function (args, localResolve, localReject) {
59 var fn;
60 if (this.failed) {
61 localReject(this.value);
62 return;
63 }
64
65 if (!isValue(this.value)) {
66 localReject(new TypeError("Cannot use null or undefined"));
67 return;
68 }
69
70 fn = args[0];
71 if (!isCallable(fn)) {
72 fn = String(fn);
73 if (!isCallable(this.value[fn])) {
74 localReject(new TypeError(fn + " is not a function"));
75 return;
76 }
77 fn = this.value[fn];
78 }
79
80 args = processArguments(slice.call(args, 1));
81 if (isPromise(args)) {
82 if (args.failed) {
83 localReject(args.value);
84 return;
85 }
86 args.done(
87 function (argsResolved) {
88 applyFn.call(this, fn, argsResolved, localResolve, localReject);
89 }.bind(this.value),
90 localReject
91 );
92 } else {
93 applyFn.call(this.value, fn, args, localResolve, localReject);
94 }
95 },
96 function (method/*, …args*/) {
97 var args, def;
98 if (this.failed) return this;
99
100 if (!isValue(this.value)) {
101 return reject(new TypeError("Cannot use null or undefined"));
102 }
103
104 if (!isCallable(method)) {
105 method = String(method);
106 if (!isCallable(this.value[method])) {
107 return reject(new TypeError(method + " is not a function"));
108 }
109 method = this.value[method];
110 }
111
112 args = processArguments(slice.call(arguments, 1));
113 if (isPromise(args)) {
114 if (args.failed) return args;
115 def = deferred();
116 args.done(
117 function (argsResolved) {
118 applyFn.call(this, method, argsResolved, def.resolve, def.reject);
119 }.bind(this.value),
120 def.reject
121 );
122 } else if (method.returnsPromise) {
123 return applyFn.call(this.value, method, args, deferred, reject);
124 } else {
125 def = deferred();
126 applyFn.call(this.value, method, args, def.resolve, def.reject);
127 }
128 return def.promise;
129 }
130 );