【发布时间】:2018-11-26 08:32:41
【问题描述】:
我的问题是如何在 node.js 或 v8 中执行等待函数结果 环境。
我们知道,node.js 是单线程非阻塞 I/O 环境。
什么是内部代码以及它是如何工作的?
示例异步函数:
async function asyncCall() {
// `getCreditorId` and `getCreditorAmount` return promise
var creditorId= await getCreditorId();
var creditAmount=await getCreditorAmount(creditorId);
}
如果您执行此函数,则首先等待 creditorId,然后使用 creditorId 调用 getCreditorAmount,然后再次在此异步函数中等待债权人 Amount。
代替异步函数,其他执行不等待,效果很好。
- 第二个问题
如果在这个例子中使用 Promise
getCreditorId().then((creditorId)=>{
getCreditorAmount(creditorId).then((result)=>{
// here you got the result
})
});
我的假设如果 async await 在内部使用 promise,那么 async 必须知道 getCreditorAmount 函数中使用哪个变量作为参数。
它是怎么知道的?
可能是我的问题毫无价值? 如果它有答案,那么我想知道答案。
感谢您的帮助。
【问题讨论】:
-
my assumption- 错了 -
至于 async/await 实际上 内部是如何工作的,我想你得看看各种 JS 引擎的源代码,看看它是如何实现的- 但是,为什么它很重要?你知道Array是如何在内部实现的吗?或日期,甚至只是对象?
-
是的,你是对的,我想要源代码,我想知道所以我问了,就是这样。
-
stackoverflow.com/questions/46908575/… 的可能重复项。不清楚您在 2 中要问什么。
标签: javascript node.js asynchronous v8