Add Promise support
See original GitHub issueIf a user wants to load user’s inventory in a function, he/she has to do this:
function sendOffer(steamId, itemIds) {
this.manager.loadUserInventory(steamId, 730, 2, true (loadInvErr, inv) => {
// blah blah blah
}
}
Eventually, if you have a lot of callbacks, it can grow up to a mess, like in these screenshot:
If this library had a Promise support, you can do it this way (that’s how I imagine it):
function sendOffer(steamId, itemIds) {
this.manager.loadUserInventory(steamId, 730, 2, true)
.then(result => {
// doing something with result
}
.catch(err => {
// handling errors here
}
}
Or (that’s the way I prefer to use it) even use async/await
with it (I think it’s awesome, though it’s not officially supported by Node.JS/ECMA standard and I have to use babel to compile it):
async function sendOffer(steamId, itemIds) {
try {
const result = await this.manager.loadUserInventory(steamId, 730, 2, true)
// doing something with the result
} catch (e) {
// handling errors
}
}
We don’t even need to use custom libraries (like q
, bluebird
etc.), because Node.JS handles Promises natively now.
My suggestion is: every time a function with callback (e.g. offer.getReceivedItems
) is called, check the last argument, and if it’s not defined, then return a Promise wrapper, if it’s defined, just call the callback directly.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:9
- Comments:9 (2 by maintainers)
Top GitHub Comments
Since v4 is out of active support, I’m (slowly) starting to migrate my stuff to Node v6 (i.e. dropping support for 4). This will be a major module release when it does happen.
Any updates on this? Looks like v3 has been on hold for quite some time.