From 5283ad1a1313176043a03ba5b688f04b227fab48 Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Thu, 23 Sep 2021 15:25:35 +0200 Subject: [PATCH] Refactored example with async await --- examples/async_compare.js | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/examples/async_compare.js b/examples/async_compare.js index e5a8daaf..7e41d63c 100644 --- a/examples/async_compare.js +++ b/examples/async_compare.js @@ -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'); +})();