0

I know this question has been asked tens if not hundreds of times, but I turned google purple and still can't find an answer that suits my case.

I have a set of three functions that I need to call one from within the other, and need each function to finish before the one that calls it continues. Currently what happens is that a function would call another one, and then continue before the function it called finished.

Most of what I have seen says to use callback functions, my problem is that my inner most function is taken from a library, thus I can not adapt it to accept a callback function as a parameter. I also saw things on timeouts, but I do not want to force my code to wait any longer than it has to, I just want it to continue once the function it calls finished.

I just want everything to work synchronously, like I am used to from any other language.

To illustrate my current code, I am using three.js and this is (basically) what I have:

firstfunction(){
    secondFunction();
}

secondFunction(){
    var loader = new THREE.JSONLoader(); //loader is an object from three.js library

    //loader.load is a three.js function that calls thirdFunction that I made. I can not make loader.load send a callback function to thirdFunction, as thirdFunction takes its arguments from three.js library
    loader.load(url, thirdFunction); 
}

thirdFunction(){ //this is a function that gets called from loader.load
    //do stuff
}

I feel like I am missing something very trivial, but as I said I can't find anything online that fits my needs.

Any help would be greatly appreciated

Oha Noch
  • 374
  • 1
  • 7
  • 22
  • Possible duplicate of [Wait for async task to finish](https://stackoverflow.com/questions/18729761/wait-for-async-task-to-finish) – JJJ Mar 24 '18 at 09:35
  • do you want to sync from third function to first function? – mr5 Mar 24 '18 at 09:35
  • 3
    *"I just want everything to work synchronously, like I am used to from any other language."* – sometimes we want things that we can't have. If you work with JavaScript you have to learn how to work with asynchronicity. – JJJ Mar 24 '18 at 09:37
  • JJJ - Thanks for the comment! What they answer in that question is either to use a callback function, which doesn't seem to fit my problem. They also mention using the .then() option, but I cant see how that can work for me, as I need the functions to be called from within each other, not just have 3 separate functions called one after the other. Furthermore the third function is reliant on an object only created inside the second function. If I am misunderstanding how this works and it might fit my needs could you please try explaining it in my specific case? – Oha Noch Mar 24 '18 at 09:59
  • mr5 - I want to syc the third function to the second one, and the second one to the first one, thus having the first one wait for the second one to finish, which in turn waits for the third one to finish, thus the first one only finishes (or continues) after the other two have finished. Using callbacks I can sync the first and the second one, but because I the third function is called from a library I cant use a callback to sync it to the second one – Oha Noch Mar 24 '18 at 10:01
  • It's unclear what the real setup is. In the example code `firstfunction` calls `secondFunction` which calls `thirdFunction`. There isn't anything there that would get called "out of order". – JJJ Mar 24 '18 at 10:03

1 Answers1

1

Even if some libraries and apis allow you to do things synchronously, this is not how javascript should work. Even if you're used to this in other languages, javascript is different.

The answer to your question probably is 'this is not possible'.

Try to lean javascript the correct way, instead of making it behave like other languages.

However, there's some hope. Once you understand fully how callback works, and structure your code around that, you might realize that Promises is a better pattern that callbacks. Promises still need a sort of call-back and are still asynchronous, but it's easier to make your code easier to read.

Then once you fully understand promises, you might be able to use async and await. New browsers support this. These 2 keywords make a lot of your code 'look' synchronous like you're used to, but it's still asynchronous. It's pretty great.

Edit

I wanna address the follow sentence more directly:

//loader.load is a three.js function that calls thirdFunction that I made. I can not make loader.load send a callback function to thirdFunction, as thirdFunction takes its arguments from three.js library

Do you really need to send that third function another callback? What kind of callback is this? Is it just another function it should always call?

You can just call another function normally from your ThirdFunction. If you need it to be variable, you can probably just assign that function to a variable. Functions can access variables from their parent scope. For example:

var callback = '..'; // assuming this a callback.

thirdFunction(){
    callback();
}

If only your second function knows what the callback should be, you might need to structure it like this:

secondFunction(){
    var loader = new THREE.JSONLoader(); //loader is an object from three.js library

    var callback = '...';

    loader.load(url, function() {
        thirdFunction(callback);
    }); 
}
Evert
  • 93,428
  • 18
  • 118
  • 189
  • I totaly agree with you , the moment you structur your code around call back and promises things get easier ,i vote up this answer – Abslen Char Mar 24 '18 at 09:53
  • 1
    More than to understand callbacks themselves (because in some APIs callbacks are synchronous), it is much more important to understand the event loop principles and also why is it needed in the first place. – Dan Macak Mar 24 '18 at 09:53
  • Thanks. I managed to use callbacks to do what I wanted between the first and second function, but because the third function was from a library I couldn't manage to send it a callback. I saw `Promises` mentioned every once and again but couldn't understand exactly what they were talking about. Also I think await gave me syntax issues, but I will try to learn them properly now and see if I can get it to work. I will report back if I can find a solution using it – Oha Noch Mar 24 '18 at 10:11
  • So I read up on promises, but I still find it hard to understand how it would get implemented in my case. I guess I just don't understand how asynchronism is supposed to be programmed. I also run into a problem because of that function from the library that I can't touch, and all the answers rely on you having full control of all the functions in order to make them compatible with the answer. Im just completely lost, so I can't be more clear, sorry about this. – Oha Noch Mar 24 '18 at 13:55