Skip to content

Commit

Permalink
Refactored example with async await
Browse files Browse the repository at this point in the history
  • Loading branch information
lpizzinidev authored Sep 23, 2021
1 parent a9c4d69 commit 5283ad1
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions examples/async_compare.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
var bcrypt = require('../bcrypt');

var start = Date.now();
bcrypt.genSalt(10, function(err, salt) {
console.log('salt: ' + salt);
console.log('salt cb end: ' + (Date.now() - start) + 'ms');
bcrypt.hash('test', salt, function(err, crypted) {
(async () => {
const start = Date.now();

// genSalt
const salt = await bcrypt.genSalt(10)
console.log('salt: ' + salt);
console.log('salt cb end: ' + (Date.now() - start) + 'ms');

// hash
const crypted = await bcrypt.hash('test', salt)
console.log('crypted: ' + crypted);
console.log('crypted cb end: ' + (Date.now() - start) + 'ms');
console.log('rounds used from hash:', bcrypt.getRounds(crypted));
bcrypt.compare('test', crypted, function(err, res) {
console.log('compared true: ' + res);
console.log('compared true cb end: ' + (Date.now() - start) + 'ms');
});
bcrypt.compare('bacon', crypted, function(err, res) {
console.log('compared false: ' + res);
console.log('compared false cb end: ' + (Date.now() - start) + 'ms');
});
});
})
console.log('end: ' + (Date.now() - start) + 'ms');

// compare
const res = await bcrypt.compare('test', crypted)
console.log('compared true: ' + res);
console.log('compared true cb end: ' + (Date.now() - start) + 'ms');

// compare
const res = await bcrypt.compare('bacon', crypted)
console.log('compared false: ' + res);
console.log('compared false cb end: ' + (Date.now() - start) + 'ms');

console.log('end: ' + (Date.now() - start) + 'ms');
})();

0 comments on commit 5283ad1

Please sign in to comment.