MCPcopy
hub / github.com/apache/pouchdb / toPromise

Function toPromise

lib/index-browser.js:102–139  ·  view source on GitHub ↗
(func)

Source from the content-addressed store, hash-verified

100}
101
102function toPromise(func) {
103 //create the function we will be returning
104 return function (...args) {
105 // Clone arguments
106 args = clone(args);
107 var self = this;
108 // if the last argument is a function, assume its a callback
109 var usedCB = (typeof args[args.length - 1] === 'function') ? args.pop() : false;
110 var promise = new Promise(function (fulfill, reject) {
111 var resp;
112 try {
113 var callback = once(function (err, mesg) {
114 if (err) {
115 reject(err);
116 } else {
117 fulfill(mesg);
118 }
119 });
120 // create a callback for this invocation
121 // apply the function in the orig context
122 args.push(callback);
123 resp = func.apply(self, args);
124 if (resp && typeof resp.then === 'function') {
125 fulfill(resp);
126 }
127 } catch (e) {
128 reject(e);
129 }
130 });
131 // if there is a callback, call it back
132 if (usedCB) {
133 promise.then(function (result) {
134 usedCB(null, result);
135 }, usedCB);
136 }
137 return promise;
138 };
139}
140
141function logApiCall(self, name, args) {
142 /* istanbul ignore if */

Callers 2

adapterFunFunction · 0.70
initFunction · 0.70

Calls 2

cloneFunction · 0.70
onceFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…