Skip to content

Commit

Permalink
Merge pull request #48 from JakobLofgren/master
Browse files Browse the repository at this point in the history
Add Promise example in Async code examples
  • Loading branch information
dcodeIO authored Dec 14, 2016
2 parents 21b7912 + de55e41 commit f0b5518
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,16 @@ To check a password:
```javascript
// Load hash from your password DB.
bcrypt.compare("B4c0/\/", hash, function(err, res) {
// res == true
// res === true
});
bcrypt.compare("not_bacon", hash, function(err, res) {
// res = false
// res === false
});

//As of bcryptjs 2.4.0, compare returns a promise if callback is omitted.
bcrypt.compare("B4c0/\/", hash)
.then(() => {console.log("Comparison was true")}) //Will be called
.catch(() => {console.log("Comparison was false")}) //Won't be called
```

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

**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.

**Note:** Since bcrypt.js 2.4.0, if the callback argument has been omitted when calling an asynchronous function, the function returns a Promise.

API
---
### setRandomFallback(random)
Expand Down

0 comments on commit f0b5518

Please sign in to comment.