question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Promise support

See original GitHub issue

If 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: callback hell illustration

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:open
  • Created 7 years ago
  • Reactions:9
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
DoctorMcKaycommented, Jun 7, 2017

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.

0reactions
srg-ncommented, Nov 26, 2022

Any updates on this? Looks like v3 has been on hold for quite some time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise - JavaScript - MDN Web Docs
A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with...
Read more >
Adding Promise Support to a Node.js Library - Brian Cline
In the built-in util package, there's a function called promisify() that allows us to convert a standard callback function into a promise based ......
Read more >
Promises | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end ... A promise represents the eventual result of an asynchronous operation....
Read more >
JavaScript Promises - W3Schools
The Promise object supports two properties: state and result. ... Both are optional, so you can add a callback for success or failure...
Read more >
ES6 - Promises - Tutorialspoint
The promise is resolved whenever any one of the add operation completes. The promise will not wait for other asynchronous operations to complete....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found