mirror of
https://github.com/Instadapp/Swap-Aggregator-Subgraph.git
synced 2024-07-29 21:57:12 +00:00
56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const jayson = require('../../');
|
|
const isFunction = require('lodash/isFunction');
|
|
|
|
/**
|
|
* Constructor for a Jayson Promise Method
|
|
* @see Method
|
|
* @class PromiseMethod
|
|
* @extends Method
|
|
* @return {PromiseMethod}
|
|
*/
|
|
const PromiseMethod = module.exports = function(handler, options) {
|
|
if(!(this instanceof PromiseMethod)) {
|
|
return new PromiseMethod(handler, options);
|
|
}
|
|
jayson.Method.apply(this, arguments);
|
|
};
|
|
require('util').inherits(PromiseMethod, jayson.Method);
|
|
|
|
module.exports = PromiseMethod;
|
|
|
|
/**
|
|
* @summary Executes this method in the context of a server
|
|
* @param {Server} server
|
|
* @param {Array|Object} requestParams
|
|
* @param {Object} [context] Optional context object passed to methods
|
|
* @param {Function} outerCallback
|
|
* @return {Promise}
|
|
*/
|
|
PromiseMethod.prototype.execute = function(server, requestParams, context, outerCallback) {
|
|
let wasPromised = false;
|
|
|
|
if(typeof(context) === 'function') {
|
|
outerCallback = context;
|
|
context = {};
|
|
}
|
|
|
|
const promise = jayson.Method.prototype.execute.call(this, server, requestParams, context, function() {
|
|
if(wasPromised) {
|
|
return; // ignore any invocations of the callback if a promise was returned
|
|
}
|
|
outerCallback.apply(null, arguments);
|
|
});
|
|
|
|
wasPromised = promise && isFunction(promise.then);
|
|
|
|
// if the handler returned a promise, call the callback when it resolves
|
|
if(wasPromised) {
|
|
return promise.then(
|
|
function(fulfilled) { outerCallback(null, fulfilled); },
|
|
function(rejected) { outerCallback(rejected); }
|
|
);
|
|
}
|
|
};
|