Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
Is it possible to run an asynchronous function without making the main function as async?

Is it possible to run an asynchronous function without making the main function as async?

function index() {
    console.log('1');
    aaa();
    console.log('3');
}

async function aaa() {
    return await bbb().then((value) => {
        console.log(value);
    });
}

async function bbb() {
    return await new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve('2');
        }, 1000);
    });
}

index();

It is possible to display:

1

2

3

Without making the index function as async?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to do something to cause console.log('3') to run after the promise returned by aaa() has been resolved.

Making index async and using await is the approach that most people find easiest to write and maintain.

async function index() {
    console.log('1');
    await aaa();
    console.log('3');
}

You could use then() instead.

function index() {
    console.log('1');
    aaa().then(
        () => { console.log('3'); }
    );
    
}
about 4 years ago · Juan Pablo Isaza Report

0

Since aaa returns a Promise, you can always interact directly with that Promise instead of using await. For example:

function index() {
  console.log('1');
  aaa().then(() => console.log('3'));
}

That way the code in the .then() callback won't execute until after the Promise returned by aaa() resolves.


For the most part, without peeking under the hood to the inner workings of JavaScript, async and await tends to just be a cleaner and simpler way of expressing this same thing. For this simple example, there really shouldn't be a meaningful difference between this:

function index() {
  console.log('1');
  return aaa().then(() => console.log('3'));
}

And this:

async function index() {
  console.log('1');
  await aaa();
  console.log('3');
}

The main difference between these and the function at the top of this answer is that the one at the top doesn't advertise that it internally has asynchronous operations. So it can't be awaited. If that's not an issue for your needs, no big deal. But even then, going with async and await still does the same thing and consuming code can simply choose not to await this function.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!