Merge pull request #237 from sbernaldev/patch-1

Use ES6 async/await in the example
This commit is contained in:
Yoni Goldberg
2018-09-16 22:21:03 +03:00
committed by GitHub

View File

@ -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