Skip to content

Commit f0b5518

Browse files
authored
Merge pull request #48 from JakobLofgren/master
Add Promise example in Async code examples
2 parents 21b7912 + de55e41 commit f0b5518

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,16 @@ To check a password:
101101
```javascript
102102
// Load hash from your password DB.
103103
bcrypt.compare("B4c0/\/", hash, function(err, res) {
104-
// res == true
104+
// res === true
105105
});
106106
bcrypt.compare("not_bacon", hash, function(err, res) {
107-
// res = false
107+
// res === false
108108
});
109+
110+
//As of bcryptjs 2.4.0, compare returns a promise if callback is omitted.
111+
bcrypt.compare("B4c0/\/", hash)
112+
.then(() => {console.log("Comparison was true")}) //Will be called
113+
.catch(() => {console.log("Comparison was false")}) //Won't be called
109114
```
110115

111116
Auto-gen a salt and hash:
@@ -117,8 +122,6 @@ bcrypt.hash('bacon', 8, function(err, hash) {
117122

118123
**Note:** Under the hood, asynchronisation splits a crypto operation into small chunks. After the completion of a chunk, the execution of the next chunk is placed on the back of [JS event loop queue](https://developer.mozilla.org/en/docs/Web/JavaScript/EventLoop), thus efficiently sharing the computational resources with the other operations in the queue.
119124

120-
**Note:** Since bcrypt.js 2.4.0, if the callback argument has been omitted when calling an asynchronous function, the function returns a Promise.
121-
122125
API
123126
---
124127
### setRandomFallback(random)

0 commit comments

Comments
 (0)