When I found q, I found in it's description, there are such demo code:
step1(function (value1) {
step2(value1, function(value2) {
step3(value2, function(value3) {
step4(value3, function(value4) {
// Do something with value4
console.log("finised: " + value4)
});
});
});
});
And
Q.fcall(step1)
.then(step2)
.then(step3)
.then(step4)
.then(function (value4) {
// Do something with value4
console.log("finished in q: " +value4);
}, function (error) {
// Handle any error from step1 through step4
console.log("err: " + err);
})
.end();
I'm wondering, how to define the 4 functions step1/step2/step3/step4
to fit these two tests?
I tried:
function step1(callback) { console.log("step1"); return "abc"; };
function step2(str, callback) { console.log("step2"); return str; };
function step3(str, callback) { console.log("step3"); return str; };
function step4(str, callback) { console.log("step4"); return str; };
and
function step1(callback) { console.log("step1"); return callback("abc"); };
function step2(str, callback) { console.log("step2"); return callback(str); };
function step3(str, callback) { console.log("step3"); return callback(str); };
function step4(str, callback) { console.log("step4"); return callback(str); };
and hope it prints:
step1
step2
step3
step4
finised: abc
step1
step2
step3
step4
finised in q: abc
But neither work.
You return via the callback
function step1(callback) { console.log("step1"); callback("abc"); };
function step2(str, callback) { console.log("step2"); callback(str); };
function step3(str, callback) { console.log("step3"); callback(str); };
function step4(str, callback) { console.log("step4"); callback(str); };