tabuC
tabuC

Reputation: 15

waiting for a synchronous funtion to finish

Let's say I have a function that returns a promise, functionThatReturnsPromise and I am using that function in a normal function, normalFunction.

normalFunction() {
    functionThatReturnsPromise().then(() => do something,);
}

I know I can wait for functionThatReturnsPromise inside normalFunction by using functionThatReturnsPromise().then(...) but what if I want normalFunction to finish when I am using my normalFunction elsewhere?

anotherNormalFunction() {
    normalFunction() // here i want this function to finish before proceeding 
                     // to the next line
}

I have tried normalFunction().then(...) and await normalFunction() but these don't seem to work. Is it possible?

Upvotes: 0

Views: 90

Answers (1)

Quentin
Quentin

Reputation: 943097

normalFunction will finish before the next line is executed.

It's just the normalFunction does nothing except trigger an asynchronous function.

If you want to wait for the asynchronous function to finish before continuing, then you need to change normalFunction so that it returns a promise that doesn't resolve until that asynchronous function has resolved.

e.g. the promise returned by that asynchronous function.

normalFunction() {
    return functionThatReturnsPromise().then(() => do something,);
}

Upvotes: 1

Related Questions