使用Bluebird.js和Twitter流的Promise和流 [英] Promises and streams using Bluebird.js and Twitter stream

查看:93
本文介绍了使用Bluebird.js和Twitter流的Promise和流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Promises和Node的新手,并且对使用流的Promises感到好奇。我可以宣传一个流吗?
使用Bluebirdjs和Twit模块我有以下内容:

I am new super new to Promises and Node and am curious about using promises with streams. Can I promisify a stream? Using Bluebirdjs and the Twit module I have the following:

var Twit = require('twit')
var Promise = require("bluebird");

var T = new Twit({
 consumer_key:         process.env.CONSUMER_KEY,
 consumer_secret:      process.env.CONSUMER_SECRET,
 access_token:         process.env.ACCESS_TOKEN,
 access_token_secret:  process.env.ACCESS_TOKEN_SECRET
})

Promise.promisifyAll(Twit);
Promise.promisifyAll(T);

var sanFrancisco = [ '-122.75', '36.8', '-121.75', '37.8' ]

T.streamAsync('statuses/filter', { locations: sanFrancisco })
.then(function(connection){
 connection.onAsync('tweet')
  .then(function (tweet) {
   console.log(tweet)
  })
});

运行此代码不会记录推文,也不会引发任何错误。似乎没有任何事情发生连接,但是没有.then承诺有效。

Running this code does not log a tweet and no error is thrown. Nothing happens the connection seems to be made but none of the .then promises work.

原始代码片段,在尝试实现 twit docs

The original snippet, before trying to implement promises found in the twit docs

var sanFrancisco = [ '-122.75', '36.8', '-121.75', '37.8' ]

var stream = T.stream('statuses/filter', { locations: sanFrancisco })

stream.on('tweet', function (tweet) {
 console.log(tweet)
})


推荐答案

我很遗憾地说,由于承诺和流之间存在根本区别,因此无效。你说你们两个都是新手,所以让我简单介绍一下这两个。

I'm sorry to say that won't work, because of a fundamental difference between a promise and a stream. You say you're new to both, so let me give you a short introduction to both.

承诺可以看作是一些可能没有到达的单一价值的占位符然而。例如,某些假设函数 getTweet()可以这样工作:

Promises can be seen as placeholders for some single value that might not have arrived yet. For instance, some hypothetical function getTweet() could work like this:

getTweet()
    .then(function (tweet) {
        //Do something with your tweet!
        console.log(tweet);
    });

但这只会让你发一条推文!要获得另一个,你必须再次调用 getTweet(),后面有一个新的 .then()那。事实上,在使用promises时,您可以保证 .then()只能调用其包含函数一次!

But this would get you just a single tweet! To get another one, you'd have to call getTweet() again, with a new .then() behind that. In fact, when working with promises you have the guarantee that a .then() calls its containing function only a single time!

Streams是连续的数据流。您不必手动询问推文,然后是另一个推文,然后是另一个推文。你打开水龙头,然后它就会一直持续到它完成或你告诉它停止。

Streams are continuous flows of data. You don't have to manually ask for a tweet, and then another one, and then another one. You open the tap, and then it just keeps coming until either it's finished or you tell it to stop.

所以总的来说,你不能宣传一个流,因为promises是针对单个值和连续数据流的流。

So, in summary, you can't promisify a stream, because promises are for single values and streams for continuous flows of data.

我假设你问了这个问题,因为你喜欢promise接口,并且想要使用类似于流的东西?根据您要实现的目标,有不同的库可以更好地处理流。 EventStream 就是一个例子。让我知道你的计划,我可以给你一个例子。

I assume you asked the question because you like the promise interface and would like to use something similar for streams? Depending on what you're trying to achieve, there are different libraries that can make working with streams nicer. EventStream is one example. Let me know what you plan and I might be able to give you an example.

这篇关于使用Bluebird.js和Twitter流的Promise和流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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