Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored example with async await #894

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
})();