UNPKG

airtable

Version:

The official Airtable JavaScript library.

54 lines 2.19 kB
"use strict"; /** * Given a function fn that takes a callback as its last argument, returns * a new version of the function that takes the callback optionally. If * the function is not called with a callback for the last argument, the * function will return a promise instead. */ /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types */ function callbackToPromise(fn, context, callbackArgIndex) { if (callbackArgIndex === void 0) { callbackArgIndex = void 0; } /* eslint-enable @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types */ return function () { var callArgs = []; for (var _i = 0; _i < arguments.length; _i++) { callArgs[_i] = arguments[_i]; } var thisCallbackArgIndex; if (callbackArgIndex === void 0) { // istanbul ignore next thisCallbackArgIndex = callArgs.length > 0 ? callArgs.length - 1 : 0; } else { thisCallbackArgIndex = callbackArgIndex; } var callbackArg = callArgs[thisCallbackArgIndex]; if (typeof callbackArg === 'function') { fn.apply(context, callArgs); return void 0; } else { var args_1 = []; // If an explicit callbackArgIndex is set, but the function is called // with too few arguments, we want to push undefined onto args so that // our constructed callback ends up at the right index. var argLen = Math.max(callArgs.length, thisCallbackArgIndex); for (var i = 0; i < argLen; i++) { args_1.push(callArgs[i]); } return new Promise(function (resolve, reject) { args_1.push(function (err, result) { if (err) { reject(err); } else { resolve(result); } }); fn.apply(context, args_1); }); } }; } module.exports = callbackToPromise; //# sourceMappingURL=callback_to_promise.js.map