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:
Sergio
2018-09-04 14:21:20 +02:00
committed by GitHub
parent 917b32dd1f
commit 761fe191b0

View File

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