Reputation: 1018
How can I improve my third function below to use both functions based on callbacks and functions based on Promises?
The first function is a standard callback implementation of "times 2".
function a(i, cb) {
return cb(null, i * 2);
}
let ans =
a(2,
function(error, result) {
if (error) console.log('a callback error ' + error.message);
return result;
}
);
console.log('a(2, cb)=' + ans);
It arrives at
a(2, cb)=4
My second function uses Promises for "times 3".
function ap(i) {
return new Promise(function(resolve, reject) {
return resolve(i * 3);
})
}
ap(2)
.then(function(result, error) {
if (error) console.log('error ' + error.message);
console.log('ap(2)=' + result);
});
It arrives at
ap(2)=6
The motivation for this question is a piece of code in which I can't seem to get statements to occur in the proper order. I'm trying with this third function to learn how to make functions with callbacks coexist with functions with Promises. This first attempt calls the above callback function and chains the above Promise function next.
let ans;
function aap(i) {
let answer = a(i, function(error, result) {
if (error) console.log('error ' + error.message);
return result;
});
ap(answer)
.then(function(result, error) {
console.log('aap ap callback result=' + result + ' error=' + error);
if (error) console.log('error ' + error.message);
ans = result;
})
}
aap(2);
console.log('aap(2)=' + ans);
It arrives at both
aap(2)=null
aap ap callback result=12 error=undefined
The first line looks like a failure. I see from the second line that if only I could control the statement order more effectively, the right answer is available. How best?
Upvotes: 0
Views: 70
Reputation: 1018
@guest271314 did a great job of answering the question. Further, they gave me the tools I needed to create a useful answer to a follow-on question. My original post hinted lightly at a follow-on question to come; it would have asked for techniques to create function apa(i) (which would call ap() first and chain it to a() (the reverse of the question asked above).
Here's what I got
function apa(i) {
ap(i)
.then(function(result, error) {
if (error) console.log('error ' + error.message)
else return a(result, function(error2, result2) {
if (error2) console.log('error ' + error2.message)
else console.log('apa(' + i + ')=' + result2);
});
});
}
apa(2);
It arrives at
apa(2)=12
If anyone cares to post better techniques for comingling functions with callbacks and functions with Promises in asynchronous environments, I'd love to read about it. By demonstration, I conclude it is possible to do.
Upvotes: 0
Reputation: 1
Try separating error
handling to second parameter of .then()
, returning ap
from aap
, setting ans
as let ans = aap(2)
function a(i, cb) {
return cb(null, i * 2);
}
function ap(i) {
return Promise.resolve(i * 3);
}
function aap(i) {
let answer = a(i, function(error, result) {
if (error) console.log("error " + error.message);
return result;
});
return ap(answer)
.then(function(result, error) {
console.log("aap ap callback result=" + result);
return result;
}, function(e) {
if (e) console.log("e " + e.message);
return e
})
}
let ans = aap(2);
ans.then(function(res) {
console.log(res);
return res
}, function err(e) {
console.log(e)
});
console.log(ans);
Upvotes: 1