I am trying to create a sequence of functions that are chained together. The following does what I want, but I want to be able to call any of the functions alone to start the sequence. For example, if I called one() it would run one(), then two(), then three(), then complete(). If I called three() it would run three then complete(). Is there a way to 'link' each function to the one below it without calling it like one().then(two).then(three).then(complete); but instead just one(), or just two(), etc?
function one() {
var d = $.Deferred();
console.log('one');
setTimeout(function() {
d.resolve();
}, 5000);
return d.promise();
}
function two() {
var d = $.Deferred();
console.log('two');
setTimeout(function() {
d.resolve();
}, 5000);
return d.promise();
}
function three() {
var d = $.Deferred();
console.log('three');
setTimeout(function() {
d.resolve();
}, 5000);
return d.promise();
}
function complete() {
console.log('done');
}
one().then(two).then(three).then(complete);