javascript 承诺在运行时环境中是如何处理的 [英] How are javascript promises handled in the runtime environment

查看:33
本文介绍了javascript 承诺在运行时环境中是如何处理的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚在运行时环境中如何处理 Promise.它们是否移动到 Web API 容器中,直到它们解析,然后在调用 .then 时推送到调用堆栈中?这是一些示例代码.Console.log 在承诺之前运行,这让我相信他们最终进入队列的过程中的某个地方.我还注意到我可以将一个函数放在 .then 中,返回的 promise 将填充该函数的参数.

I'm trying to figure out how promises are handled in the runtime environment. Are they moved into the web API container until they resolve and then pushed into the callstack when .then is called? Here is some example code. Console.log runs before the promises which leads me to believe somewhere along the way they end up in the queue. I also noticed I can put a function in a .then and the returned promise will fill that functions parameters.

// asynchronous test
let promiseWhatever = new Promise( function(resolve, reject){

 // variable to be chained later and passed in function argument
  let chainedVariable = 'I am chained';
  resolve(chainedVariable);
  reject('rejected promise');
});

let promiseMe = function(promiseResult) {
  let message = `${promiseResult} to my computer`;
  return Promise.resolve(message); 
  // resolves here to be passed onto the second chained then
};


function hello() {


  promiseWhatever
  .then(promiseMe) 
  // how does promiseMe take in the result for its argument?

  // then returns another promise and you can chain them
  .then( function(fulfilled){
    console.log(fulfilled);
  }) // is fullfilling the code to display the string to the console.
  .catch( function(err) {
    console.log(err);
  });

  console.log('hello'); // logs first to the console

};

hello();

推荐答案

首先承诺只是一个通知方案.底层异步操作(任何代码解析或拒绝承诺)通常在 Javascript 之外(使用本机代码),例如传入的 webSocket 消息或 ajax 响应或类似的东西.

First off a promise is just a notification scheme. The underlying asynchronous operation (whatever code resolves or rejects the promise) that would typically be outside of Javascript (using native code) such as an incoming webSocket message or an ajax response or something like that.

所有承诺引擎都使用事件队列.当一个promise被解决时,他们将一个事件发布到事件队列中以触发适当的.then().catch()处理程序被调用.语言或承诺规范并不要求它,但许多实现使用特殊的事件队列进行承诺回调,该队列与其他类型的事件队列一起检查.

All promise engines use the event queue. When a promise is resolved, they post an event to the event queue in order to trigger the appropriate .then() or .catch() handler to be called. It is not required by the language or promise specification, but a number of implementations use a special event queue for promise callbacks that is checked along with other types of event queues.

promise 规范要求 .then().catch() 处理程序总是在当前事件循环代码完成后异步调用,即使承诺立即解决.这就是为什么您的 console.log('hello') 显示在 .then() 处理程序中的 console.log() 之前.这是设计使然,这样做是为了在 Promise 调用它的处理程序时保持一致性(总是在当前事件循环代码完成之后).

It is required by the promise specification that a .then() or .catch() handler is always called asynchronously AFTER the current event loop code has finished even if the promise is resolved immediately. That's why your console.log('hello') shows before the console.log() inside the .then() handler. This is by design and is done in order to create consistency on when a promise calls it's handlers (always after the current event loop code completes).

它们是否移动到 Web API 容器中,直到它们解析,然后在调用 .then 时推送到调用堆栈中?

Are they moved into the web API container until they resolve and then pushed into the callstack when .then is called?

当一个 promise 被解决时,一个事件被插入到事件队列中,这将导致在当前事件循环代码完成后(在未来的事件循环中)调用适当的 .then() 回调循环).

When a promise is resolved an event is inserted into the event queue which will cause the appropriate .then() callbacks to get called after the current event loop code has completed (on a future event loop cycle).

不清楚您所说的Web API 容器"是什么意思,因此我无法对此发表评论.

It's not clear what you mean by "web API container" so I can't comment on that.

我还注意到我可以在 .then 中放置一个函数,返回的 promise 将填充该函数的参数

I also noticed I can put a function in a .then and the returned promise will fill that functions parameters

是的,这就是 Promise 的工作方式..then() 处理程序被传递一个表示承诺的解析值的参数..catch() 处理程序被传递一个表示拒绝原因的参数.

Yes, this is how promises work. A .then() handler is passed a single argument that represents the resolved value of the promise. A .catch() handler is passed a single argument that represents the reject reason.

这篇关于javascript 承诺在运行时环境中是如何处理的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆