Skip to content

Commit

Permalink
docs: Add async/await example
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
nwoltman committed Aug 24, 2017
1 parent ea9b6ea commit 11ce7f5
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ npm install uid-generator --save

```js
const UIDGenerator = require('uid-generator');
const uidgen = new UIDGenerator(); // Default is a 128-bit UID encoded in base58

// Async with `await`
await uidgen.generate(); // -> 'B1q2hUEKmeVp9zWepx9cnp'

// Async with promise
uidgen.generate()
.then(uid => console.log(uid)) // -> 'PXmRJVrtzFAHsxjs7voD5R'
.catch(err => { throw err; });

// Async with callback
const uidgen = new UIDGenerator(); // Default is a 128-bit UID encoded in base58
uidgen.generate((err, uid) => {
if (err) throw err;
console.log(uid); // -> '4QhmRwHwwrgFqXULXNtx4d'
});

// Async with promise
const uidgen2 = new UIDGenerator(null, 10);
uidgen2.generate()
.then(uid => console.log(uid)) // -> 'N13n1cjVP2'
.catch(err => { throw err; });

// Sync
const uidgen3 = new UIDGenerator(256, UIDGenerator.BASE62);
uidgen3.generateSync();
// -> 'x6GCX3aq9hIT8gjhvO96ObYj0W5HBVTsj64eqCuVc5X'
uidgen.generateSync(); // -> '8Vw3bgbMMzeYfrQHQ8p3Jr'
```


Expand Down Expand Up @@ -102,14 +102,11 @@ Asynchronously generates a UID.

**Returns**: `?Promise` - A promise that will resolve with the UID or reject with an error. Returns nothing if the `cb` parameter is specified.

**Callback Example**
**`async`/`await` Example**

```js
const uidgen = new UIDGenerator();
uidgen.generate((err, uid) => {
if (err) throw err;
// Use uid here
});
const uid = await uidgen.generate(); // This must be inside an async function
```

**Promise Example**
Expand All @@ -123,6 +120,16 @@ uidgen.generate()
.catch(err => { throw err; });
```

**Callback Example**

```js
const uidgen = new UIDGenerator();
uidgen.generate((err, uid) => {
if (err) throw err;
// Use uid here
});
```

---

### uidgen.generateSync() ⇒ `string`
Expand Down

0 comments on commit 11ce7f5

Please sign in to comment.