Adding support for promises
See original GitHub issueI’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:
- Created 6 years ago
- Reactions:2
- Comments:13 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
?@mrksbnch It seems that
util.promisify
changes the context of the wrapped function, so thatthis
no longer references thesecurePassword
instance (thethis
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, whereoriginal
is called with athis
, which in that context refers to the global scope:https://github.com/nodejs/node/blob/1c28dfa09a443bab9f72fd1d89a4f645de8c5a42/lib/internal/util.js#L229