为什么我不能将 Promise.resolve 与渗透实例一起使用?
Why can't I use Promise.resolve with an osmosis instance?
我试图理解为什么这些 console.log
语句的行为不同。我希望他们的行为相同:
使用节点 7。考虑以下情况:
1. Promise.resolve(对象)
Promise.resolve
按我的预期处理对象:
Promise.resolve({ a: `hello` }).then(console.log) // { a:'hello' }
2。直接 console.log
来自库的 class 个实例。
如果我存储一个 Osmosis 实例,我可以 console.log 它:
const osmosis = require(`osmosis`)
console.log(new osmosis.get(url))
/* { prev:
{ instance: Osmosis:1,
name: 'get',
args: [ 'http://www.google.com', , ],
getURL: [Function: getURLArg],
url: 'http://www.google.com',
params: undefined,
cb: [Function: Get],
next: [Circular] } }
*/
3。 Promise.resolve(class实例)
但是如果我尝试解析一个 Osmosis 实例,我看不到奇偶校验:
Promise.resolve(new osmosis.get(url)).then(console.log) // nothing
这是怎么回事?我对 Promise.resolve()
有什么误解吗...?或者 console.log
?
鉴于 [1] 中的行为,为什么 [3] 的记录与 [2] 不同?
上下文:我认为我的近期实际目标与回答这个问题无关紧要。但你去这里以防万一。我看不出库本身会如何影响最终示例的输出。不过,这是关于 new osmosis.get()
的文档:http://rchipka.github.io/node-osmosis/Osmosis.html#toc1__anchor
new osmosis.get(url)
不执行异步 http 请求。它实例化了一个 scraper 的实例,它可以用一组声明性指令来构建,并在以后的任意时间告诉 "run"。
出于多种原因,我希望能够在承诺链中构建这组指令。
最主要的是,将指令定义分解为更易于测试和理解的不同函数将是最简单的方法。例如而不是 osmosis.get(url).set({some stuff}).find(@something)
,我想:
function defineSearch(instance){
return instance.set({some stuff})
}
function definePath(instance) {
return instance.find(@something)
}
Promise.resolve(new osmosis.get(url))
.then(defineSearch)
.then(definePath)
.then(instance => instance.run())
文档很糟糕并且使用了非常规的技术。 new osmosis.get(url)
returns 不是 Osmosis
instance but a Command
one. And those do have a then
method.
当你将一些东西传递给 Promise.resolve
时,它会被测试是否是 thenable,如果它看起来像一个 promise,它就会被尝试同化:一个回调被传递到then
解决新承诺的方法。
因此,当您执行 Promise.resolve(new osmosis.get(url))
时,您会得到一个未解决的承诺,该承诺将在调用 then
回调时实现(当您 运行 命令时发生)。在你的情况下,它永远不会。
您的特定问题的解决方案是根本不使用承诺(因为您没有做任何异步操作):
definePath(defineSearch(new osmosis.get(url))).run())
但您可能还应该报告一个错误,该错误 Command
看起来像 promises with being properly thenable,它破坏了 ES6 中的很多东西。
我试图理解为什么这些 console.log
语句的行为不同。我希望他们的行为相同:
使用节点 7。考虑以下情况:
1. Promise.resolve(对象)
Promise.resolve
按我的预期处理对象:
Promise.resolve({ a: `hello` }).then(console.log) // { a:'hello' }
2。直接 console.log
来自库的 class 个实例。
如果我存储一个 Osmosis 实例,我可以 console.log 它:
const osmosis = require(`osmosis`)
console.log(new osmosis.get(url))
/* { prev:
{ instance: Osmosis:1,
name: 'get',
args: [ 'http://www.google.com', , ],
getURL: [Function: getURLArg],
url: 'http://www.google.com',
params: undefined,
cb: [Function: Get],
next: [Circular] } }
*/
3。 Promise.resolve(class实例)
但是如果我尝试解析一个 Osmosis 实例,我看不到奇偶校验:
Promise.resolve(new osmosis.get(url)).then(console.log) // nothing
这是怎么回事?我对 Promise.resolve()
有什么误解吗...?或者 console.log
?
鉴于 [1] 中的行为,为什么 [3] 的记录与 [2] 不同?
上下文:我认为我的近期实际目标与回答这个问题无关紧要。但你去这里以防万一。我看不出库本身会如何影响最终示例的输出。不过,这是关于 new osmosis.get()
的文档:http://rchipka.github.io/node-osmosis/Osmosis.html#toc1__anchor
new osmosis.get(url)
不执行异步 http 请求。它实例化了一个 scraper 的实例,它可以用一组声明性指令来构建,并在以后的任意时间告诉 "run"。
出于多种原因,我希望能够在承诺链中构建这组指令。
最主要的是,将指令定义分解为更易于测试和理解的不同函数将是最简单的方法。例如而不是 osmosis.get(url).set({some stuff}).find(@something)
,我想:
function defineSearch(instance){
return instance.set({some stuff})
}
function definePath(instance) {
return instance.find(@something)
}
Promise.resolve(new osmosis.get(url))
.then(defineSearch)
.then(definePath)
.then(instance => instance.run())
文档很糟糕并且使用了非常规的技术。 new osmosis.get(url)
returns 不是 Osmosis
instance but a Command
one. And those do have a then
method.
当你将一些东西传递给 Promise.resolve
时,它会被测试是否是 thenable,如果它看起来像一个 promise,它就会被尝试同化:一个回调被传递到then
解决新承诺的方法。
因此,当您执行 Promise.resolve(new osmosis.get(url))
时,您会得到一个未解决的承诺,该承诺将在调用 then
回调时实现(当您 运行 命令时发生)。在你的情况下,它永远不会。
您的特定问题的解决方案是根本不使用承诺(因为您没有做任何异步操作):
definePath(defineSearch(new osmosis.get(url))).run())
但您可能还应该报告一个错误,该错误 Command
看起来像 promises with being properly thenable,它破坏了 ES6 中的很多东西。