0

I have a problem with calling a non-async function from the function method. Is there any way to do this?

Example:

async function myMethod()
{
  console.log('In async method');
  nonasyncmethod(); 
}

function nonasyncmethod()
{
  console.log('In non-async method');
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
ANKIT SHARMA
  • 83
  • 1
  • 2
  • 10
  • If you are talking about *object methods* then you likely need to call the method as `this.nonasyncmethod()`. But that's how methods work in general and has nothing to do with async. If that doesn't solve your problem please provide a [mcve]. – Felix Kling Mar 28 '19 at 07:41
  • Possible duplicate of [Call An Asynchronous Javascript Function Synchronously](https://stackoverflow.com/questions/9121902/call-an-asynchronous-javascript-function-synchronously) – Syed mohamed aladeen Mar 28 '19 at 07:42
  • Is your example code have valid syntax ? – Sudhir Ojha Mar 28 '19 at 07:46
  • Looks good to me. – Francis G Mar 28 '19 at 07:47
  • @SyedMohamedAladeen no my question is opposite of that question – ANKIT SHARMA Mar 28 '19 at 07:50
  • Yes you can call a non-async method from async method. And your javascript function declarations are wrong. Their name should be preceded by the "function" keyword. – RK_15 Mar 28 '19 at 07:50
  • Given your edit, the code will work as it is. Again, if you have problem, then you should provide a [mcve]. – Felix Kling Mar 28 '19 at 08:18

1 Answers1

0

Yes, you can call both asynchronous and synchronous functions from within an async function - they do not have to be all asynchronous (of course the primary reason for having an async function is for asynchronous functions, but they work with synchronous functions as well):

async function myMethod() {
  console.log('In async method');
  nonasyncmethod();
}

function nonasyncmethod() {
  console.log('In non-async method');
}

myMethod();
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79