js - How to call an async function within a Promise .then()

async/await javascript
convert promise to async/await
javascript promise
async/await promise
async/await vs promises
nodejs async/await
async arrow function
async call

First, I have to mention that I already look through many questions in stackoverflow, but many doesn't answer my question. Not to mention many doesn't even have an answer.

How do I achieve the following, making sure functionB() executes after functionA() finishes?

Note: I do not want to convert my async functions to new Promise(resolve=>{...}) because I'll have to convert the someServiceThatMakesHTTPCall() as well, and any other async functions within the call stack, which is a big change.

  function functionThatCannotHaveAsyncKeyword() {
      functionA()
        .then(async function() {
            await functionB();
        })
        .then(function() {
            console.log('last');
        });
  }

  async function functionA() {
      console.log('first');
      await someServiceThatMakesHTTPCall();
  }

  async function functionB() {
      console.log('second');
      await someServiceThatMakesHTTPCall();
  }

Your approach using await in an async then callback will work, but it's unnecessarily complex if all you want to do is call the async function and have its result propagate through the chain. But if you are doing other things and want the syntax benefit of async functions, that's fine. I'll come back to that in a moment.

async functions returns promises, so you just return the result of calling your function:

function functionThatCannotHaveAsyncKeyword() {
  functionA()
    .then(function() {
        return functionB(someArgument);
    })
    .then(function() {
        console.log('last');
    }); // <=== Note: You need a `catch` here, or this function needs
        // to return the promise chain to its caller so its caller can
        // handle errors
}

If you want to pass functionA's resolution value into functionB, you can do it even more directly:

functionA()
  .then(functionB)
  // ...

When you return a promise from a then callback, the promise created by the call to then is slaved to the promise you return.

Example:

const wait = (duration, ...args) => new Promise(resolve => {
  setTimeout(resolve, duration, ...args);
});

async function functionA() {
  await wait(500);
  return 42;
}

async function functionB() {
  await wait(200);
  return "answer";
}

functionB()
.then(result => {
  console.log(result); // "answer"
  return functionA();
})
.then(result => {
  console.log(result); // 42
})
.catch(error => {
  // ...handle error...
});

Async/await, async function wait ( ) { await new Promise ( resolve => setTimeout we need to call async wait() and wait to get 10 the .then(function) and returns the result you got from resolve(result) . async function foo () { await 1 } is equivalent to: function foo () { return Promise.resolve (1).then ( () => undefined) } Code after each await expression can be thought of as existing in a .then callback. In this way a promise chain is progressively constructed with each reentrant step through the function.


You can use promise inside the first method as

function functionThatCannotHaveAsyncKeyword() {
    return new Promise(async(resolve, reject)=> {
          await functionA();
          await functionB();
          console.log('last');
          resolve();    
      });
  }

  async function functionA() {
      console.log('first');
      await someServiceThatMakesHTTPCall();
  }

  async function functionB() {
      console.log('second');
      await someServiceThatMakesHTTPCall();
  }

Promises chaining, In other words, do they behave the same way in any circumstances, for any handler functions? promise . then ( f1 ) . catch ( f2 ) ; One can catch async errors by changing the code slightly as below : According to the specs, calling . catch() is just sugar for .then(undefined� Whats is async The Async statement is to create async method in JavaScript.The function async is always return a promise.That promise is rejected in the case of uncaught exceptions, otherwise resolved to the return value of the async function. Whats Await in JavaScript The await is a statement and used to wait for a promise to resolve or reject.


if someServiceThatMakesHTTPCall is async you can avoid all that by doing the following:

function functionThatCannotHaveAsyncKeyword() {
    functionA()
        .then(function() {
            return functionB()
        })
        .then(function() {
            console.log('last');
        });
  }

  function functionA() {
      console.log('first');
      return someServiceThatMakesHTTPCall();
  }

  function functionB() {
      console.log('second');
      return someServiceThatMakesHTTPCall();
  }

Making asynchronous programming easier with async and await , They make your asynchronous code less "clever" and more readable. If you use the async keyword before a function definition, you can then use await within the function. When you await a promise, the function is paused in a non-blocking way until the promise settles. If the promise fulfills, you get the value back. The word “async” before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically. For instance, this function returns a resolved promise with the result of 1; let’s test it: async function f() { return 1; } f().then(alert); // 1.


async function, We then declare an async function and await for the promise to resolve before logging function scaryClown() { return new Promise(resolve => { setTimeout(() In the following example, the second time with call our msg function we pass in a� Here the first .then shows 1 and returns new Promise(…) in the line (*).After one second it resolves, and the result (the argument of resolve, here it’s result * 2) is passed on to handler of the second .then.


Async functions, js - How to call an async function within a Promise .then() - javascript Note: I do not want to convert my async functions to new Promise(resolve=>{}) because� Callbacks are the rescuing agents while writing async code in Node JS. But, they look ugly. JS community came up with intuitive solution called Promises to write async code elegantly


Exploring Async/Await Functions in JavaScript ← Alligator.io, As we can see from the above result, each function call and… What we can do to make sure a() completes first and then b() and then c() . You wrap the whole function in a Promise, and instead of calling the callback, you call resolve (or reject if there is an error). The function returns this Promise object. Again, let’s try to