Note: This exercise is specifically asking me to do this with callbacks and not promises

Currently, I took some time to checkout if there were in any similar solutions to the problem that I had (which I did find, but it wasn't quite clear on how to go about doing it as the solutions seemed a bit more complicated and did not seem to work). For this exercise, I am told that I need to make a function called inOrder that accepts two callbacks and invokes them in order. I kind of understand how callbacks work (you place a function within the parameters of another function), but what is tripping me up the setTimeout function. I am given two functions ( logOne & logTwo) that contain setTimeOut and will console.log one and two in order regardless of how many times it is invoked. Essentially, the goal is to use the callback to work around the asynchronous nature of JavaScript and the SetTimeOut that will console.log two first and then one (at times). I was able to work around it, but my solution seems inefficient and I'm not sure if I am understanding the scope of callbacks. Here's my solution to this:

}, Math.random() * 1000);
function logTwo () {  setTimeout(function() {
}, Math.random() * 1000);

function inOrder (callback1, callback2) {
    }, 1000);

 inOrder(logOne, function() {

So this solution will always result in one being printed out first then second shortly after. Any suggestions on if I'm thinking about this correctly or how to do it more efficiently would be greatly appreciated. Thanks for your time.

0 Answers