我应该在等待事件发生时明确地创建一个承诺吗?

时间:2015-12-13 00:15:04

标签: javascript node.js rss promise bluebird

我正在编写一个函数来获取rss feed并返回上次更新的日期:

var FeedParser = require('feedparser'),
Promise = require('bluebird'),
request = require('request');

function getDate() {
    var deferred = Promise.defer();
    var feed = new FeedParser();
    request('rss.xml');
    .on('response',function() {
        this.pipe(feed);
    });

    feed.on('meta',function() {
        deferred.resolve(this.meta.date);
    });

    return deferred.promise;
}

这是反模式吗?这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

你可以稍微简化一下。

for (index, users) in upimage.enumerate() {
    let radians = CGFloat(M_PI) * 2.0 / CGFloat(upimage.count) * CGFloat(index)

    ......

    let centerx = self.view.bounds.width / 2.0
    let radius =  currentuserpic.frame.size.width / 2.0
    let centery = self.view.bounds.height / 2.0

    let pointx = centerx + cos(radians) * radius
    let pointy = centery + sin(radians) * radius

    ......
}