How to make the following chainable methods work?
I.e promise
method should wrap the data returned from get
into promise.
// I need to do both:
// return object directly
return client.get(args);
// return promise
return client.get(args).promise();
Why? There's a library that uses this signature and I need to mock it for testing.
Edit: answer applied
client.get = function(args) {
let obj = {foo: 'bar'}; // get data from somewhere
obj.promise = function() {
return new Promise((resolve, reject) => {
console.log(this); // {foo: 'bar'}
resolve(this);
// TODO: reject logic
});
};
return obj;
};