I'm trying to convert this code to a version without "async - await". Code converted to use (all) promises without async and await. But my console print undefined for time. Can someone help me? Its for a homework of my College. Thanks very much
(async() => {
function exibirErro(err) {
console.log(err.message);
}
const f1 = async(tempo) => {
return new Promise((resolve, reject) => {
const f = () => reject(new Error('Rejeitado'));
console.log(`Aguardando ${tempo} segundos...`);
setTimeout(f, tempo);
});
};
const f2 = async(x) => {
if ((Math.random() * 10) % 2 == 0) {
throw new Error('Ops');
}
return Promise.resolve(1000 * x);
};
const f3 = async() => {
const r = await new Promise(resolve => resolve(2));
const t = await f2(r);
await f1(t);
};
try {
await f3();
} catch (err) {
exibirErro(err);
}
})();
My code:
function exibirErro(err) {
console.log(err.message);
}
function f1(tempo) {
return new Promise((resolve, reject) => {
const f = () => reject(new Error('Reject'));
console.log(`wait ${tempo} seconds...`);
setTimeout(f, tempo);
})
.then((x) => {
if ((Math.random() * 10) % 2 == 0) {
throw new Error('Ops');
}
return Promise.resolve(1000 * x);
})
.then((t) => {
return Promisse.resolve(f1(t));
}).catch((e) => {
exibirErro(e);
})
}
f1();
Starting with your wrapper function -
(async() => {
First we have a normal synchronous function, nothing to change here -
function exibirErro(err) {
console.log(err.message);
}
Next we have function f1 which uses the async keyword but does not await anything. All you have to do here is remove the async keyword -
const f1 = async(tempo) => {
return new Promise((resolve, reject) => {
const f = () => reject(new Error('Rejeitado'));
console.log(`Aguardando ${tempo} segundos...`);
setTimeout(f, tempo);
});
};
Next we have function f2, which again uses async but does not await anything. Simply remove async -
const f2 = async(x) => {
if ((Math.random() * 10) % 2 == 0) {
throw new Error('Ops');
}
return Promise.resolve(1000 * x);
};
Then we have function f3 that awaits two things, r and t -
const f3 = async() => {
const r = await new Promise(resolve => resolve(2));
const t = await f2(r);
await f1(t); // missing return
};
We can rewrite f3 using .then -
const f3 = () =>
new Promise(resolve => resolve(2)).then(r =>
f2(r).then(t =>
f1(t)
)
)
Finally you have try/catch -
try {
await f3();
} catch (err) {
exibirErro(err);
}
Simply use .catch instead -
f3().catch(exibirErro)
And the closing syntax for the async wrapper function. The wrapper will not be needed in the final version.
})();
Here it is all together now -
function exibirErro(err) {
console.log(err.message);
}
const f1 = tempo => {
return new Promise((resolve, reject) => {
const f = () => reject(new Error('Rejeitado'));
console.log(`Aguardando ${tempo} segundos...`);
setTimeout(f, tempo);
});
};
const f2 = x => {
if ((Math.random() * 10) % 2 == 0) {
throw new Error('Ops');
}
return Promise.resolve(1000 * x);
};
const f3 = () => {
return new Promise(resolve => resolve(2)).then(r =>
f2(r).then(t =>
f1(t)
)
)
}
f3().catch(exibirErro)