Reputation: 1490
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.
Upvotes: 0
Views: 65
Reputation: 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
})
Upvotes: 1
Reputation: 655
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.
Upvotes: 0