我的问题是在node.js或v8环境中,执行如何等待函数结果。
我们知道,node.js是单线程非阻塞I/O环境。
内部代码是什么,它是如何工作的?
以下是异步函数示例:
我们知道,node.js是单线程非阻塞I/O环境。
内部代码是什么,它是如何工作的?
以下是异步函数示例:
async function asyncCall() {
// `getCreditorId` and `getCreditorAmount` return promise
var creditorId= await getCreditorId();
var creditAmount=await getCreditorAmount(creditorId);
}
如果您执行此函数,则首先等待creditorId,然后仅在此异步函数中使用creditorId调用getCreditorAmount,并再次等待creditor Amount。
除异步函数外的其他执行不等待,这样可以正常工作。
- 第二个问题
如果对此示例使用Promise
getCreditorId().then((creditorId)=>{
getCreditorAmount(creditorId).then((result)=>{
// here you got the result
})
});
我的假设是,如果async await在内部使用promise,那么async必须知道在getCreditorAmount函数中使用哪个变量作为参数。
它怎么知道?
也许我的问题毫无价值? 如果有答案,我想知道答案。
感谢您的帮助。