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!')); 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