Reputation: 33
I don't know how to convert this Callback into a Promise (result, error). Can you show me how it's made and explain with comments?
function calcular()
{
let conta = 0;;
var N1 = parseInt(document.getElementById("num1").value);
var opção = document.getElementById("op").value;
var N2 = parseInt(document.getElementById("num2").value);
if (opção === "+")
{
conta = N1 + N2;
}
}
function MostrarResultado(conta)
{
document.getElementById("result").innerHTML = "O resultado da sua conta é = " + conta;
}
function Executar(callback)
{
callback(calcular());
}
function xpto()
{
setTimeout(function() {Executar(MostrarResultado)}, 3000);
}
Upvotes: 2
Views: 40
Reputation: 294
its not 100% clear on what you want but here is a quick example of what i think your asking for
so to make your function use promises instead of callback you could do something like this
function promiseFunction(){
return new Promise(resolve =>{
resolve(result);
}
}
and when you want to get the data that the function returns you could do this
function getInfoFromFunction(){
promiseFunction(arg).then(result => {
console.log(result)
});
}
Upvotes: 2