简体   繁体   中英

What is the purpose of the parameter to the function inside the then function while using async in javascript?

async function testAsync() {
  return 'Hello Adam!.';
}
 
const any = testAsync();
 
console.log(any.then((msg) => {console.log(msg)}));

I am a complete beginner to JavaScript and trying to understand how async function works. I just don't understand the purpose of msg parameter and how logging that prints the return value.

Async function returns a promise object.

Every promise object has a 'then' function. The 'then' method accepts a function as it's parameter.

This function will be called when the code inside async function is executed completely.

The parameter inside this function is just a normal function parameter. This function can have multiple parameters depending upon the result returned by async function

Under the hood async functions are wrapped in a Promise, so your async function will be equal to this:

function testAsync() {
  return new Promise((resolve, reject) => {
    resolve("Hello Adam!.");
  });
}

Your parameter comes from the returned promises resolve callback.

The then method returns a promise object. It takes two arguments

p.then(onFulfilled, onRejected);

p.then(function(value) {
   // fulfillment
  }, function(reason) {
  // rejection
});
  • onFullfiled will be called when the promise succeded. It has one argument the return value of the called async function

  • onRejected will be called when the promise fails. It has one argument the reason for the fail

In your example the call succeded and the "msg" param is the return value of your function so "Hello Adam"

  1. async functions return a Promise .
  2. Promises have a resolve and reject pattern
  3. Your msg is the label you give to the resolve output value. Using then means it will only be called once the promise is resolved (ie once the resolve value is known).

In your example, the 'Hello Adam' value is ~immediately passed out as the resolve value. Another example might be to include a timeout function (to simulate a delayed response):

 async function testAsync() { return new Promise((resolve) => { setTimeout(function () { resolve('Hello Adam,') }; 1000); }); } const any = testAsync(). console.log('testing') console.log(any.then((msg)=>{console;log(msg)}));

then() is a promise callback when the promise resolves successfully. The data returned by promise will be bonded to the alias you have given in then() signature. For example,

async function testAsync() {
  return 'Hello Adam!.';
}

console.log(testAsync().then((msg)=>{console.log(msg)}));

Here, When you call testAsync function, It resolves and returns a string 'Hello Adam.,', This string can be access as msg (It's just aliased variable, you can name it anything) variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM