MCPcopy Index your code
hub / github.com/apache/pouchdb / toPromise

Function toPromise

lib/index.js:106–143  ·  view source on GitHub ↗
(func)

Source from the content-addressed store, hash-verified

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

Callers 1

adapterFunFunction · 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…