简体   繁体   English

为什么这个已解析的Q承诺会以一个promise响应valueOf()?

[英]Why does this resolved Q promise responds to valueOf() with a promise?

I have an object whose value comes from an AJAX request. 我有一个对象,其值来自AJAX请求。 I'm converting it to a promise and am encountering some behaviour with promises that I don't expect. 我正在将其转换为一个承诺,并且遇到一些我没有想到的承诺行为。 I have an example here that exhibits the same behaviour, but I'm substituting Q.all() for my AJAX request. 我这里有一个表现出相同行为的示例,但是我用Q.all()代替了我的AJAX请求。

(Thing = function(){
  var promise;
  var refresh = function() {
    promise = Q.all(["a", "b"]);
  };

  var fetch = function(i) {
    return promise.then(function(promiseVal){
      return promiseVal[i];
    });
  };

  refresh();

  return {
    "fetch": fetch,
    "refresh": refresh,
    "promise": promise
  };

}());

Thing is executed on load and runs "refresh" to populate itself initially. 事物在加载时执行,并运行“刷新”以初始填充自身。 The point of the "fetch" function is that my async request (Q.all in this case) returns a promise for an array, but what I really want is a promise for elements in the array (eg a promise for "a" or a promise for "b"). “获取”功能的重点是我的异步请求(在本例中为Q.all)返回对数组的承诺,但我真正想要的是对数组中元素的承诺(例如,对“ a”或对“ b”的承诺)。 So I'm expecting Thing.fetch(1) to return a promise for "b". 因此,我期望Thing.fetch(1)返回对“ b”的承诺。

Thing.fetch(1) does return a promise, but if I do Thing.fetch(1).valueOf() it returns a promise, not "b" as I was expecting. Thing.fetch(1)确实返回了一个承诺,但是如果我执行Thing.fetch(1).valueOf()则它返回了一个承诺,而不是我期望的“ b”。 If I do: 如果我做:

Thing.fetch(1).then(function(foo){
  console.log(foo);
});

it will print "b" on the console. 它将在控制台上显示“ b”。

If I do Thing.promise.valueOf() it returns the array, so the promise is resolved when I call "fetch". 如果我执行Thing.promise.valueOf()它将返回数组,因此当我调用“ fetch”时,promise就会解决。

So my question is why doesn't valueOf() return a value when I call it on the promise returned by "fetch"? 所以我的问题是,为什么当我在“ fetch”返回的promise上调用valueOf()时不返回值?

It seems your promise is not resolved yet. 您的承诺似乎尚未解决。

It's not well documented, but scattered over the wiki pages I found: 它没有很好的文档记录,但是分散在我发现的Wiki页面上:

The valueOf call returns the promise itself by default. 默认情况下,valueOf调用将返回promise。

The valueOf method is useful for providing information about the promise in the same turn of the event loop. valueOf方法对于在事件循环的同一回合中提供有关诺言的信息很有用。 For example, resolved promises return their resolution value and rejections return an object that is recognized by isRejected. 例如,已解析的Promise返回其分辨率值,而拒绝则返回isRejected可以识别的对象。

If the promise is fulfilled, promise.valueOf() returns the fulfilled value. 如果实现了promise,则promise.valueOf()返回已实现的值。 If the promise is or has forwarded to a deferred promise, it returns most recently deferred promise. 如果该承诺是或已经转发到延期的承诺,它将返回最近的延期的承诺。 For rejected promises, promise.valueOf() returns a sentinel object with {rejectedPromise: true, reason: {}} 对于被拒绝的承诺, promise.valueOf()返回一个带有{rejectedPromise: true, reason: {}}的哨兵对象

When you're doing Thing.fetch(1).then(console.log); 当您在做Thing.fetch(1).then(console.log); it will of course print "b" on the console - that's the resolve value which is passed to the callback. 它当然会在控制台上显示"b" -这是传递给回调的解析值。 But notice that it will invoke the log function in the future! 但是请注意,它将在将来调用log函数!

Because the value of the promise is an array. 因为promise的值是一个数组。 The value of the function passed to "then" will be some value. 传递给“ then”的函数的值将是某个值。 Your fetch method is not really returning a promise, it is resolving a promise. 您的提取方法实际上并没有返回承诺,而是在解决承诺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM