mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-11-03 03:36:26 +08:00
Merge pull request #237 from sbernaldev/patch-1
Use ES6 async/await in the example
This commit is contained in:
@ -16,12 +16,16 @@ const myEmitter = new MyEmitter();
|
|||||||
myEmitter.emit('error', new Error('whoops!'));
|
myEmitter.emit('error', new Error('whoops!'));
|
||||||
|
|
||||||
// 'throwing' an Error from a Promise
|
// 'throwing' an Error from a Promise
|
||||||
return new Promise(function (resolve, reject) {
|
const addProduct = async (productToAdd) => {
|
||||||
return DAL.getProduct(productToAdd.id).then((existingProduct) => {
|
try {
|
||||||
if(existingProduct != null)
|
const existingProduct = await DAL.getProduct(productToAdd.id);
|
||||||
reject(new Error("Why fooling us and trying to add an existing product?"));
|
if (existingProduct !== null) {
|
||||||
});
|
throw new Error("Product already exists!");
|
||||||
});
|
}
|
||||||
|
} catch (err) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Code example – Anti Pattern
|
### Code example – Anti Pattern
|
||||||
|
|||||||
Reference in New Issue
Block a user