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.

Adding support for promises

See original GitHub issue

I’d like to request support for the Promise API. Your code example together with async / await would look like following then.

const securePassword = require('secure-password')

// Initialise our password policy
const pwd = securePassword()

const doHashingAndVerify = async function () {
  const userPassword = Buffer.from('my secret password')

  // Register user
  const hash = await pwd.hash(userPassword) 

  // Save hash somewhere
  const result = await pwd.verify(userPassword, hash)

  if (result === securePassword.INVALID) return console.log('Imma call the cops')
  if (result === securePassword.VALID) return console.log('Yay you made it')
  if (result === securePassword.VALID_NEEDS_REHASH) {
    console.log('Yay you made it, wait for us to improve your safety')

    const improvedHash = await pwd.hash(userPassword)
  }
}

doHashingAndVerify()
  .then(() => console.log('succesful'))
  .catch(err => throw err)

Really enjoyed your JSConf talk btw!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
watsoncommented, May 31, 2017

Instead of a wrapper which needs to be maintained, wouldn’t it be easier to simply promisify the module in your code via util.promisify?

1reaction
emilbayescommented, Oct 12, 2017

@mrksbnch It seems that util.promisify changes the context of the wrapped function, so that this no longer references the securePassword instance (the this part here), and instead refers to the global scope. I don’t know much about promises or how promisify works, but it seems that the issue lies here, where original is called with a this, which in that context refers to the global scope:

https://github.com/nodejs/node/blob/1c28dfa09a443bab9f72fd1d89a4f645de8c5a42/lib/internal/util.js#L229

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using promises - JavaScript - MDN Web Docs
Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function. Imagine a function, ...
Read more >
Adding Promise Support to a Node.js Library - Brian Cline
There are a few different ways to convert callbacks to use promises. In the built-in util package, there's a function called promisify() that ......
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 >
Native Support for Promises in Node.js - Stack Overflow
Node.js added native support for Promises since it merged with io.js. ... A host of new ES6 features, such as Promises, were added...
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 >

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