async,await + await,catch playground
try async,await error handling with await,catch
by Juan Muñoz
JavaScript 1.7
async function main() {
const number = await new Promise((resolve,reject) => {
setTimeout(() => {
const temp = Math.floor(Math.random() * 10);
if (temp < 2) {
reject(`${temp} is not enough. #1`);
} else {
resolve(temp);
}
}, 1000);
});
if (number < 5) {
throw(`${number} is not enough. #2`);
} else {
result = number;
}
return result;
}
let i = 0;
while(i < 10) {
main()
.then(v => console.log(v, 'from caller then'))
.catch(e => console.log(e, 'from caller catch'));
i++;
}