JSFiddle - React, Tailwind, and code Playground
by Abdul Ahmad
JavaScript
function async(success) {
return new Promise((res, rej) => {
setTimeout(() => {
if (success) res('success');
else rej('fail');
}, 1000);
});
}
async function login(errorCallback) {
try {
return await async();
} catch (e) {
errorCallback(e);
}
}
async function one() {
const cb = (e) => { throw ({ message: 'some failure' }); };
try {
const res = await login(cb);
console.log('RESULT IN ONE', res);
} catch (e) {
console.log('ERROR IN ONE', e);
}
}
async function two() {
const cb = (e) => {};
try {
const res = await login(cb);
console.log('RESULT IN TWO', res);
} catch (e) {
console.log('ERROR IN TWO', e);
}
}
one();
two();