mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-28 03:25:55 +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!'));
|
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) => {
|
const existingProduct = await DAL.getProduct(productToAdd.id);
|
||||||
if(existingProduct != null)
|
if (existingProduct != null)
|
||||||
reject(new Error("Why fooling us and trying to add an existing product?"));
|
reject(new Error("Why fooling us and trying to add an existing product?"));
|
||||||
});
|
}
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Code example – Anti Pattern
|
### Code example – Anti Pattern
|
||||||
|
|||||||
Reference in New Issue
Block a user