'difference with try/catch+await and await+then/catch in JS
Are there any differences in performance or error handling between these two codes?
#1
# promises is array of async function
myResult = [];
try {
result = await Promise.all(promises);
result.forEach(function (arg) {
myResult.push(arg);
});
} catch(err) {
console.log("Error");
}
#2
# promises is array of async function
myResult = [];
await Promise.all(promises)
.then(result => {
result.forEach(function(arg) {
myResult.push(arg);
});
})
.catch(err => {
console.log("Error");
});
Solution 1:[1]
The catch block in the second code is the part of the promise chain and has it's own benefit. It can also return another Promise. Since it returns another promise it can be chained too. Also await is unnecessary in the second case
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | brk |
