admin管理员组

文章数量:1415119

My question is how execution wait for function result in node.js or v8 environment.

We know, node.js is single thread non blocking I/O environment.

What is internal code and how it work?

Example async function:

async function asyncCall() {      
 // `getCreditorId` and `getCreditorAmount` return promise
  var creditorId= await getCreditorId(); 
  var creditAmount=await getCreditorAmount(creditorId);

}

If you execute this function then first wait for creditorId then call getCreditorAmount using creditorId and again wait from creditor Amount in this async function only.

Instead of async function other execution not wait, that works fine.

  1. Second question

If use promise for this example

getCreditorId().then((creditorId)=>{
   getCreditorAmount(creditorId).then((result)=>{
      // here you got the result
  })
});

My assumption if async await use promise internally then async must must know which varibale use in getCreditorAmount function as parameter.

How it know ?

Might be my question is worthless? If it has a answer then i want to know the ans.

Thanks for help.

My question is how execution wait for function result in node.js or v8 environment.

We know, node.js is single thread non blocking I/O environment.

What is internal code and how it work?

Example async function:

async function asyncCall() {      
 // `getCreditorId` and `getCreditorAmount` return promise
  var creditorId= await getCreditorId(); 
  var creditAmount=await getCreditorAmount(creditorId);

}

If you execute this function then first wait for creditorId then call getCreditorAmount using creditorId and again wait from creditor Amount in this async function only.

Instead of async function other execution not wait, that works fine.

  1. Second question

If use promise for this example

getCreditorId().then((creditorId)=>{
   getCreditorAmount(creditorId).then((result)=>{
      // here you got the result
  })
});

My assumption if async await use promise internally then async must must know which varibale use in getCreditorAmount function as parameter.

How it know ?

Might be my question is worthless? If it has a answer then i want to know the ans.

Thanks for help.

Share Improve this question edited Jun 17, 2018 at 4:40 hong4rc 4,1214 gold badges23 silver badges42 bronze badges asked Jun 17, 2018 at 3:46 ManMan 7721 gold badge6 silver badges23 bronze badges 6
  • 1 your async/await code in babel transpiler – Jaromanda X Commented Jun 17, 2018 at 3:50
  • my assumption - is wrong – Jaromanda X Commented Jun 17, 2018 at 3:51
  • as for how async/await actually works internally, I guess you'd have to look at the source code of the various JS Engines, to see exactly how it is implemented - but, why is it important? Do you know how Array is implemented internally? Or Date, or even just Object? – Jaromanda X Commented Jun 17, 2018 at 3:56
  • 1 yes you are right i want source code ,i want know so i asked ,thats it. – Man Commented Jun 17, 2018 at 4:02
  • 1 Possible duplicate of stackoverflow./questions/46908575/… . It's unclear what you're asking in 2. – Estus Flask Commented Jun 17, 2018 at 4:03
 |  Show 1 more ment

2 Answers 2

Reset to default 5

async-await uses Generators to resolve and wait for Promise.

await is asynchronous in async-await, when piler reach at await it stops executing and push everything into event queue and continue with synchronous code after async function. Example

function first() {
    return new Promise( resolve => {
        console.log(2);
        resolve(3);
        console.log(4);
    });
}

async function f(){
    console.log(1);
    let r = await first();
    console.log(r);
}

console.log('a');
f();
console.log('b');

Since await is asynchronous thus every other thing before await happens as usual

a
1
2
4
b
// asynchronous happens
3

async/await is just a Generator. Take a look at these docs if you would like to learn more.

Generators

Async Function

本文标签: javascriptHow async await internally workStack Overflow