运行异步代码时,Promise执行程序回调在哪里? [英] Where does Promise executor callback live when asynchronous code is being run?

查看:115
本文介绍了运行异步代码时,Promise执行程序回调在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个Promise构造函数可以使用一个executor callback function,这个问题是关于当执行者回调函数具有异步代码时,该回调函数在执行空间中的位置.

A Promise constructor function can take a executor callback function and this question is about where does that callback function live in execution space when the executor callback function has asynchronous code.

详细信息:

Promise对象表示一个可能尚不可用的值,但将来会在某个时候解析.它允许您编写异步代码,例如调用远程Web服务,您将创建Promise对象,该对象表示将来Web服务将返回的数据.

A Promise object represents a value that may not be available yet, but will be resolved at some point in the future. It allows you to write asynchronous code like making a call to a remote web service, you will create a Promise object which represents the data that will be returned by the web service in future.

在实际数据可用之前,Promise对象就像实际数据的代理一样.

Until the actual data is available, the Promise object acts like a proxy to the actual data.

下面的代码段描述了这种情况:

Code snippet below depicts this situation :

function getRandomJoke(){
  return new Promise((resolve, reject) => {
    const request = new XMLHttpRequest();

    request.open('GET', 'https://api.icndb.com/jokes/random');
    request.onload = () => {
      if (request.status === 200) {
        resolve(request.response); // we got data here, so resolve the Promise
      } else {
        reject(Error(request.statusText)); // status is not 200 OK, so reject
      }
    };

    request.onerror = () => {
      reject(Error('Error fetching data.')); // error occurred, reject the  Promise
    };

    request.send(); // send the request
  });
}

根据此线程中的讨论,两者承诺创建和执行程序回调函数的执行发生在主线程上,只有解决回调将在事件循环的下一个计时周期执行.

As per the discussion in this thread, both Promise creation and executor callback function execution happens on the main thread and only resolve callback will be executed on the next tick of event loop.

如果是这种情况,则上述代码段的执行程序函数具有进行API调用的异步代码-也将一直挂在主线程上,直到API返回数据为止.

If that is the case, above code snippet's executor function has asynchronous code of making API call - will that also hang on to the main thread till API returns data.

推荐答案

如果是这种情况,则上述代码段的执行程序函数具有进行API调用的异步代码-也将一直挂在主线程上,直到API返回数据为止.

If that is the case, above code snippet's executor function has asynchronous code of making API call - will that also hang on to the main thread till API returns data.

不,它不会挂任何东西. request.send()是异步且非阻塞的.当您调用request.send()时,它将启动请求的发送(将请求移交给OS),然后立即返回. Promise执行程序函数本身会返回,然后主线程可以自由执行其想做的任何事情.

No, it won't hang anything. request.send() is asynchronous and non-blocking. When you call request.send() it initiates the sending of the request (turns it over to the OS) and then immediately returns. The promise executor function itself returns and then the main thread is free to do whatever else it wants to do.

同时,一段时间之后,基础的TCP操作将导致事件被插入事件队列中.当该事件由主线程处理时(当它什么都不做,并且该事件进入事件队列)时,请求对象将生成onloadonerror事件,并且相应的侦听器将解决或拒绝诺言.这将使诺言在将来的价格变动中调用它的.then().catch()处理程序.

Meanwhile, sometime later, the underlying TCP operation will cause an event to get inserted into the event queue. When that event gets processed by the main thread (when it's not doing anything else and this event gets its turn in the event queue), the request object will then generate either an onload or an onerror event and the corresponding listener will resolve or reject the promise. That will set in motion the promise calling it's .then() or .catch() handlers on a future tick.

以这种方式使用请求在promise executor函数内部绝对没有什么不同,因为它在node.js中的其他任何地方都没有.它启动异步,非阻塞操作,您为回调设置了一些侦听器,然后nodejs解释器可以自由地执行其他操作,直到稍后某个完成事件发生并且某些回调被调用为止.

Using request this way is absolutely no different inside a promise executor function as it is anywhere else in node.js. It initiates the asynchronous, non-blocking operation, you set up some listeners for callbacks and then the nodejs interpreter is free to do other things until sometime later when a completion event occurs and some callbacks get called.

运行异步代码时,Promise执行程序回调在哪里驻留?

Where does Promise executor callback live when asynchronous code is being run?

住在哪里?"的意思还不清楚. Promise执行程序功能与任何其他功能一样.它被同步调用,并在返回new Promise()构造函数时返回,并且创建了一个承诺,供调用代码使用.稍后的某个时候,诺言将被解决或被拒绝(通常).初始化和设置promise的一部分是运行promise执行程序功能,但这只是创建promise的一步.如果您恰巧在其中执行异步操作,则该功能不会阻塞.

It's not super clear what you mean by "where does it live?". The promise executor function is just like any other function. It's called synchronously and when it returns the new Promise() constructor returns and there's a promise created that the calling code can use. Sometime later that promise will get resolved or rejected (usually). Part of initializing and setting up the promise is running of the promise executor function, but that's just one step in creating the promise. That function doesn't block if you happen to do something asynchronous in it.

这篇关于运行异步代码时,Promise执行程序回调在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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