mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-11-01 18:46:54 +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!'));
|
||||
|
||||
// 'throwing' an Error from a Promise
|
||||
return new Promise(function (resolve, reject) {
|
||||
return DAL.getProduct(productToAdd.id).then((existingProduct) => {
|
||||
if(existingProduct != null)
|
||||
reject(new Error("Why fooling us and trying to add an existing product?"));
|
||||
});
|
||||
});
|
||||
const addProduct = async (productToAdd) => {
|
||||
try {
|
||||
const existingProduct = await DAL.getProduct(productToAdd.id);
|
||||
if (existingProduct !== null) {
|
||||
throw new Error("Product already exists!");
|
||||
}
|
||||
} catch (err) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Code example – Anti Pattern
|
||||
|
||||
Reference in New Issue
Block a user