0

I want to execute two lines of JS after each other, in order, line by line. How would this be achieved? In the case I need to implement this, arrow functions are not viable.

functionThatTakesSomeTime();
console.log("execute this after the function");

is there even a way to do this, and in that case how?

MoltasDev
  • 65
  • 1
  • 7
  • What's wrong with the pseudo-code you have? – Patrick Roberts Oct 12 '19 at 20:38
  • 1
    Is the first function synchronic? – yahavi Oct 12 '19 at 20:39
  • could you provide the context? Where do you need to call the function and log? And some explanations about function. – Blind Kai Oct 12 '19 at 20:43
  • @BlindKai In my front-end, I am doing a request to my API and then do a redirect. However, the redirect is executed first which doesn't work since the request to API is supposed to send a cookie which will allow the redirect to work. – MoltasDev Oct 12 '19 at 20:50
  • 1
    Then either `await` the async function (which you have to do inside an async function) or use the Promise API. – VLAZ Oct 12 '19 at 20:52
  • @MoltasDev could you please check, what's the nature of function? If the function is asynchronous you could try to wrap it in `Promise` or use `async`/`await` if it already is. – Blind Kai Oct 12 '19 at 20:52

1 Answers1

0
async function someFunc() {
    await functionThatTakesSomeTime();
    console.log("execute this after the function");
}

someFunc();

If you are referring to an asynchronous function (functionThatTakesSomeTime), you can use async/await

krish
  • 186
  • 1
  • 5