为什么不能将Promise.resolve作为函数调用? [英] Why can't Promise.resolve be called as a function?

查看:721
本文介绍了为什么不能将Promise.resolve作为函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些东西正在扰乱我和我的同事。考虑以下内容......

  const {map,compose} = require('ramda'); 

compose(
console.log,
map(Math.tan)
)([1,2,3]);
$ b compose(
console.log,
map(v => Promise.resolve(v))
)([4,5,6]);

compose(
console.log,
map(Promise.resolve)
)([7,8,9]);

正如您所期望的那样,输出1,2和3的正切值以及解决承诺3,4和5,但我的问题是......为什么第三次休息?为什么Promise.resolve的行为与其他函数一样?

  [1.5574077246549023,-2.185039863261519,-0.1425465430742778] 
[Promise {4},Promise {5},Promise {6}]
/home/xxx/node_modules/ramda/src/internal/_map.js:6
结果[idx] = fn(functor [idx]);
$

在_map(/ home / xxx / node_modules / ramda / src)解析(<匿名>)
时,PromiseResolve调用非对象
/internal/_map.js:6:19)
在地图(/home/xxx/node_modules/ramda/src/map.js:57:14)
在/ home / xxx / node_modules / ramda /src/internal/_dispatchable.js:39:15
在/home/xxx/node_modules/ramda/src/internal/_curry2.js:20:46
在f1(/ home / xxx / node_modules /ramda/src/internal/_curry1.js:17:17)
在/home/xxx/node_modules/ramda/src/internal/_pipe.js:3:27
在/ home / xxx /在Object中,node_modules / ramda / src / internal / _arity.js:5:45
< anonymous> (/home/xxx/b.js:20:6)
在Module._compile(module.js:569:30)


解决方案

Promise.resolve 引用 resolve 没有上下文对象的函数。



你想用适当的上下文对象调用它。这可以通过调用该上下文对象来完成


  • ,如 v => (Promise)中的 Promise.resolve(v)




所以,这可以奏效:

  compose(
console.log,
map(Promise.resolve.bind(Promise))
)([7,8,9]);






请记住,Javascript没有类。函数没有所有者。对象可以将函数存储在它们的属性中,但这并不意味着该函数将由该对象拥有。

另一种方法是显式设置上下文对象,其中 Function#call 函数#apply

  function(v){
var resolve = Promise.resolve;
return resolve.call(Promise,v);
}






也许最好的例子是关注方法以外的内容:

  function Foo(){
this.bar = {some:value };
this.baz = function(){return this.bar; };
}

var f = new Foo();
var b = f.bar;
var z = f.baz;

这里 b 指的是 {some:value} 没有 {some:value} 神奇地知道 f 存储对它的引用。这应该是显而易见的。



z 也是如此。它存储了一个没有函数知道 f 也引用它的函数。这在理论上应该是显而易见的。



调用 z()将产生与调用 f.baz(),尽管被调用的函数是相同的。只有上下文不同。


Something that is bugging me and my colleague. Consider the following...

const {map, compose} = require('ramda');

compose(
  console.log,
  map(Math.tan)
)([1,2,3]);

compose(
  console.log,
  map(v=>Promise.resolve(v))
)([4,5,6]);

compose(
  console.log,
  map(Promise.resolve)
)([7,8,9]);

As you would expect, the tan of 1, 2 and 3 are output and so are the promises resolving 3, 4 and 5. But my question is... why does the third break? Why doesn't Promise.resolve behave in the same way as any other function?

[ 1.5574077246549023, -2.185039863261519, -0.1425465430742778 ]
[ Promise { 4 }, Promise { 5 }, Promise { 6 } ]
/home/xxx/node_modules/ramda/src/internal/_map.js:6
    result[idx] = fn(functor[idx]);
                  ^

TypeError: PromiseResolve called on non-object
    at resolve (<anonymous>)
    at _map (/home/xxx/node_modules/ramda/src/internal/_map.js:6:19)
    at map (/home/xxx/node_modules/ramda/src/map.js:57:14)
    at /home/xxx/node_modules/ramda/src/internal/_dispatchable.js:39:15
    at /home/xxx/node_modules/ramda/src/internal/_curry2.js:20:46
    at f1 (/home/xxx/node_modules/ramda/src/internal/_curry1.js:17:17)
    at /home/xxx/node_modules/ramda/src/internal/_pipe.js:3:27
    at /home/xxx/node_modules/ramda/src/internal/_arity.js:5:45
    at Object.<anonymous> (/home/xxx/b.js:20:6)
    at Module._compile (module.js:569:30)

解决方案

Promise.resolve refers to the resolve function without context object.

You want to call it with the proper context object. This can be done

  • by calling it on that context object, as in v => Promise.resolve(v), or
  • by creating a bound version of it, as in Promise.resolve.bind(Promise)

So, this would work:

compose(
  console.log,
  map(Promise.resolve.bind(Promise))
)([7,8,9]);


Remember that Javascript does not have classes. Functions have no owner. Objects can store functions in their properties, but that does not mean the function is owned by that object.

Another way is setting the context object explicitly, with Function#call or Function#apply:

function (v) {
    var resolve = Promise.resolve;
    return resolve.call(Promise, v);
}


Maybe it's best illustrated by focusing on something other than a method:

function Foo() {
    this.bar = {some: "value"};
    this.baz = function () { return this.bar; };
}

var f = new Foo();
var b = f.bar;
var z = f.baz;

here b refers to {some: "value"} without {some: "value"} magically "knowing" that f stores a reference to it. This should be obvious.

The same is true of z. It stores a function without that function "knowing" that f also references it. This should be just as obvious, in theory.

Calling z() will yield different results than calling f.baz(), even though the called function is the same one. Only the context is different.

这篇关于为什么不能将Promise.resolve作为函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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