为什么我不能将Promise.resolve与一个渗透实例一起使用? [英] Why can't I use Promise.resolve with an osmosis instance?

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

问题描述

我试图理解为什么这些 console.log 语句的行为不同。我希望它们的行为相同:

I am trying to understand why these console.log statements behave differently. I expect them to behave the same:

使用节点7。请考虑以下情况:

Using Node 7. Consider the following cases:

1。 Promise.resolve(object)

Promise.resolve 如我所愿处理对象:

Promise.resolve({ a: `hello` }).then(console.log) // { a:'hello' }

2。直接从库中直接 console.log 一个类实例。

2. Directly console.log a class instance from a library.

如果我存储了一个渗透实例可以console.log它:

If I store an Osmosis instance I can console.log it:

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(类实例)

但是如果我尝试解析一个渗透实例,我看不到奇偶性:

But if I try to resolve an Osmosis instance I don't see parity:

Promise.resolve(new osmosis.get(url)).then(console.log) // nothing

这是怎么回事?我是否误解了 Promise.resolve() ...?还是 console.log

What's going on here? Am I misunderstanding something about Promise.resolve()...? Or console.log?

上下文:我认为我眼前的实际目标与回答这个问题无关。但是,这里您以防万一。我看不到有关库本身的任何内容如何影响最终示例的输出。不过,这是关于 new osmosis.get()的文档: http://rchipka.github.io/node-osmosis/Osmosis.html#toc1__anchor

Context: I don't think my immediate practical goals matter to answering this question. But here you go just in case. I don't see how anything about the library itself should affect the output of the final example. Here's the docs on that new osmosis.get() though: http://rchipka.github.io/node-osmosis/Osmosis.html#toc1__anchor

new osmosis.get(url)不执行异步http请求。它实例化了scraper的一个实例,该实例可以由一组声明性指令构建,并被告知在以后任意任意时间运行。

new osmosis.get(url) doesn't perform an async http request. It instantiates an instance of the scraper, which can be built up with a set of declarative instructions and told to "run" at some arbitrary later time.

我想成为之所以能够在承诺链中建立这组指令,有几个原因。

I want to be able to build this set of instructions in a promise chain for several reasons.

最主要的是,这是将指令定义分解为易于测试和理解的不同功能的最简单方法。例如而不是 osmosis.get(url).set({some something})。find(@something),我想:

The main one is that it would be the easiest way to break up the instruction definitions into different functions that are easier to test and understand. e.g. instead of osmosis.get(url).set({some stuff}).find(@something), I'd like to:

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)返回的不是 Osmosis 实例,但 Command 一个。那些确实有 then 方法

The documentation is horrible and uses techniques that are rather unconventional. What new osmosis.get(url) returns is not an Osmosis instance but a Command one. And those do have a then method.

当您将某些内容传递给 Promise.resolve 时,它将被测试为可否,并且,如果它看起来像一个承诺,它就会被同化:回调被传递到然后方法将解决新的承诺。

When you pass something to Promise.resolve, it is tested for being a thenable or not, and if it looks like a promise it is tried to be assimilated: a callback is passed into the then method which will resolve the new promise.

因此,当您执行 Promise.resolve(new osmosis.get(url)),您会得到一个未解决的承诺,该承诺将在调用 then 回调时实现(在您运行命令)。

So when you do Promise.resolve(new osmosis.get(url)), you get back an unresolved promise that will fulfill when the then callback is called (which happens when you run the command). In your case, it never does.

您的特定问题的解决方案是根本不使用promise(因为您没有做任何异步操作):

The solution to your particular problem is not to use promises at all (since you're not doing anything asynchronous):

definePath(defineSearch(new osmosis.get(url))).run())

,但是您可能还应该报告一个错误,该错误使 Command 看起来像是可以正确使用的承诺,这会破坏ES6中的许多内容。

but you probably should also report a bug that Commands look like promises with being properly thenable, which breaks a lot of things in ES6.

这篇关于为什么我不能将Promise.resolve与一个渗透实例一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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