To run three functions serially, meaning that each function runs only after the previous one completes, you can use callbacks, Promises, or async/await (in JavaScript or other similar languages). Here’s how you can implement it using each of these methods:
1. Using Callbacks (Traditional Approach)
javascript
function function1(callback) {
console.log(“Function 1 is running…”);
setTimeout(() => {
console.log(“Function 1 complete.”);
callback(); // Call the next function after this one is done
}, 1000);
}function function2(callback) {
console.log(“Function 2 is running…”);
setTimeout(() => {
console.log(“Function 2 complete.”);
callback();
}, 1000);
}function function3(callback) {
console.log(“Function 3 is running…”);
setTimeout(() => {
console.log(“Function 3 complete.”);
callback();
}, 1000);
}// Run the functions serially
function1(() => {
function2(() => {
function3(() => {
console.log(“All functions are complete.”);
});
});
});
2. Using Promises
javascript
function function1() {
return new Promise((resolve) => {
console.log(“Function 1 is running…”);
setTimeout(() => {
console.log(“Function 1 complete.”);
resolve(); // Resolves and triggers the next function
}, 1000);
});
}function function2() {
return new Promise((resolve) => {
console.log(“Function 2 is running…”);
setTimeout(() => {
console.log(“Function 2 complete.”);
resolve();
}, 1000);
});
}function function3() {
return new Promise((resolve) => {
console.log(“Function 3 is running…”);
setTimeout(() => {
console.log(“Function 3 complete.”);
resolve();
}, 1000);
});
}// Run the functions serially using chaining
function1()
.then(() => function2())
.then(() => function3())
3. Using `async/await` (Modern and Cleaner Approach)
javascript
async function function1() {
console.log(“Function 1 is running…”);
return new Promise((resolve) => {
setTimeout(() => {
console.log(“Function 1 complete.”);
resolve();
}, 1000);
});
}async function function2() {
console.log(“Function 2 is running…”);
return new Promise((resolve) => {
setTimeout(() => {
console.log(“Function 2 complete.”);
resolve();
}, 1000);
});
}async function function3() {
console.log(“Function 3 is running…”);
return new Promise((resolve) => {
setTimeout(() => {
console.log(“Function 3 complete.”);
resolve();
}, 1000);
});
}// Run functions serially with async/await
async function runFunctions() {
await function1();
await function2();
await function3();
console.log(“All functions are complete.”);
}runFunctions();
Explanation:
– Callbacks: Each function takes a callback, and the callback for the next function is executed once the previous one completes.
– Promises: Functions return Promises, and .then() is used to chain the functions in order, ensuring they run serially.
– Async/Await: The `await` keyword makes the JavaScript engine wait for the function to resolve before continuing to the next one, making the code cleaner and more readable.
In all three cases shared by hire tech firms, each function waits for the previous one to complete before starting, ensuring the functions run in sequence. The async/await version is often the most concise and readable.