0

This is for vanilla JavaScript. I wan't a function to execute as soon as another function has completely finished. Right now I actually have something that works, but I suppose it can be done better:

setTimeout(function () {
  // does things with the variables a, b(array) and c
}, 500);
function happySunshine(some inparameters){
  // calculates and sets variables a, b(array) and c
}

The happySunshine function must unfortunately lay after my setTimeout function in the order, this cannot change.

Now this works because the happySunshine function will have executed and completed it's tasks during the half second that setTimeout is set to wait before executing.

So... I wan't to know. Is there a way to make a function that is first in the order, wait until another function (later in the order) is completely finished before it executes?

Important to note is that these functions cannot be in the same scope.

5
  • Why is setTimeout() call necessary? Commented Apr 10, 2017 at 9:16
  • Why not declaring the function before and calling it from happySunshine()? You can even pass the parameters if you need to. Commented Apr 10, 2017 at 9:18
  • The setTimeout() is the way I have solved the problem right now, I actually don't want it. Commented Apr 10, 2017 at 9:23
  • @mligor as I wrote, the functions must be in that order. The happySunshine must come after in order. Commented Apr 10, 2017 at 9:25
  • 1
    That I understand, but your function inside of setTimeout is called after happySunshine. I don't see the difference between calling this function directly from happySunshine, then from setTimeout. you can declare a function like variable: var myFun = function(){ ... code ..} and call it then from happySunshine. Internally that is what JS engine is doing with setTimeout Commented Apr 10, 2017 at 9:33

2 Answers 2

1

You can return Promise.resolve() or Promise constructor from happySunshine() with value set to an object having properties and values a, b, c.

function happySunshine(/* some inparameters */){
  // calculates and sets variables a, b(array) and c
  // return new Promise(function(resolve, reject) {
       // do stuff
       // resolve({a:a, b:[], c:c});
  // });
  return Promise.resolve({a:a, b:[], c:c});
}

happySunshine(/* some inparameters */)
.then(function({a, b, c}) {
  // does things with the variables a, b(array) and c
})
Sign up to request clarification or add additional context in comments.

3 Comments

This looks interesting but I don't really understand it. Can you explain what it does? Thanks for helping me!
Thanks guest271314
0

Have you tried something like this

function initialFunction(){
//Do you magic here
happySunShine(params);
}

Your happySunShine() method should be available globally otherwise this will not work.

1 Comment

Thanks Spharah, but I don't thik this will work because "//Do you magic here" is dependent that happySunShine(params) will be executed before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.