node-trello icon indicating copy to clipboard operation
node-trello copied to clipboard

Add Promise compatibility

Open awesomejerry opened this issue 8 years ago • 4 comments
trafficstars

  • Update dependency should
  • Return a promise for then chaining

awesomejerry avatar Aug 29 '17 10:08 awesomejerry

Any consideration to upgrade node version in travis? 0.10 is kind of legacy. If not, I'll try to add Promise polyfill.

awesomejerry avatar Aug 29 '17 11:08 awesomejerry

0.10 is kind of legacy.

Understatement of the year! 😄 Definitely, just merged #49, but feel free to upgrade support further if you need to.

Can you also include an update to the README? Thanks!

adunkman avatar Sep 29 '17 20:09 adunkman

I've updated the README!

awesomejerry avatar Sep 29 '17 23:09 awesomejerry

If I'm not mistaken, it doesn't exactly work as shown in the README.

It says this is valid

// Use it as a promise
t.get("/1/members/me", { cards: "open" })
  .then(function(data) { console.log(data); });

However, the params don't seem to be taken into account.

On the other hand, this works as intended

t.get("/1/members/me", { cards: "open" }, (err, data) => console.log(err, data))

My guess is, the problem comes from those lines https://github.com/awesomejerry/node-trello/blob/master/lib/node-trello.js#L50-L53.

 if (arguments.length === 3) {
    callback = argsOrCallback;
    args = {};
  }

You're checking if the function receives 3 arguments, the third argument is considered the callback. This is incorrect in the first case: it calls the function with the arguments ("GET", "/1/members/me", { cards: "open" })

I suppose changing it to arguments.length === 4 should work as intended.

EDIT: On second thought, this might not work either if the function is called with

t.get("/1/members/me", (err, data) => console.log(err, data))

Because it will indeed produce the 3 arguments ("GET", "/1/members/me", (err, data) => console.log(err, data))

EDIT2: maybe this would work https://github.com/dfdeagle47/node-trello/blob/master/lib/node-trello.js#L53-L60

Cheers

dfdeagle47 avatar Oct 16 '17 10:10 dfdeagle47