Skip to content

Commit 5283ad1

Browse files
authored
Refactored example with async await
1 parent a9c4d69 commit 5283ad1

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

examples/async_compare.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
var bcrypt = require('../bcrypt');
22

3-
var start = Date.now();
4-
bcrypt.genSalt(10, function(err, salt) {
5-
console.log('salt: ' + salt);
6-
console.log('salt cb end: ' + (Date.now() - start) + 'ms');
7-
bcrypt.hash('test', salt, function(err, crypted) {
3+
(async () => {
4+
const start = Date.now();
5+
6+
// genSalt
7+
const salt = await bcrypt.genSalt(10)
8+
console.log('salt: ' + salt);
9+
console.log('salt cb end: ' + (Date.now() - start) + 'ms');
10+
11+
// hash
12+
const crypted = await bcrypt.hash('test', salt)
813
console.log('crypted: ' + crypted);
914
console.log('crypted cb end: ' + (Date.now() - start) + 'ms');
1015
console.log('rounds used from hash:', bcrypt.getRounds(crypted));
11-
bcrypt.compare('test', crypted, function(err, res) {
12-
console.log('compared true: ' + res);
13-
console.log('compared true cb end: ' + (Date.now() - start) + 'ms');
14-
});
15-
bcrypt.compare('bacon', crypted, function(err, res) {
16-
console.log('compared false: ' + res);
17-
console.log('compared false cb end: ' + (Date.now() - start) + 'ms');
18-
});
19-
});
20-
})
21-
console.log('end: ' + (Date.now() - start) + 'ms');
16+
17+
// compare
18+
const res = await bcrypt.compare('test', crypted)
19+
console.log('compared true: ' + res);
20+
console.log('compared true cb end: ' + (Date.now() - start) + 'ms');
21+
22+
// compare
23+
const res = await bcrypt.compare('bacon', crypted)
24+
console.log('compared false: ' + res);
25+
console.log('compared false cb end: ' + (Date.now() - start) + 'ms');
26+
27+
console.log('end: ' + (Date.now() - start) + 'ms');
28+
})();

0 commit comments

Comments
 (0)