mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-27 19:17:13 +08:00
Use ES6 async/await in the example
Hi, I'm a beginner developer. I think now it's more clear with ES6 Async/await and arrow function. I didn't use try-catch because throw. I'm just practising! Any suggestion is very welcome
This commit is contained in:
@ -16,12 +16,11 @@ 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) => {
|
||||
const existingProduct = await DAL.getProduct(productToAdd.id);
|
||||
if (existingProduct != null)
|
||||
reject(new Error("Why fooling us and trying to add an existing product?"));
|
||||
}
|
||||
```
|
||||
|
||||
### Code example – Anti Pattern
|
||||
|
||||
Reference in New Issue
Block a user