【问题标题】:Javascript: Promise.all doesn't seem to be resolving [duplicate]Javascript:Promise.all 似乎没有解决 [重复]
【发布时间】:2016-02-11 10:02:10
【问题描述】:

我正在尝试使用feedparser-promised 映射一些RSS 提要,但我对Promise.all() 的调用没有返回任何内容。 (var的使用来自我的REPL,不支持constlet

var feedparser = require('feedparser-promised');
var R = require('ramda');
var URLS = ['http://feeds.gawker.com/gizmodo/full',
            'http://kotaku.com/vip.xml',
           'http://www.medievalhistories.com/feed/'];

var getAllFeeds = (urls) => {
    promises = R.map(feedparser.parse, urls);
    Promise.all(promises)
        .then((itemArrs) => itemArrs)
        .catch((err) => {throw new Error(err)});
}

var x = getAllFeeds(URLS);
console.log(x);

xundefined 的形式返回。如果我登录promises,它会按预期显示为 Promises 数组。我错过了什么?

【问题讨论】:

  • 即使我将.then 替换为.then((itemArrs) => console.log(itemArrs)),itemArrs 也永远不会被记录

标签: javascript promise es6-promise


【解决方案1】:

x 未定义,因为getAllFeeds 是一个arrow function with statement body,它没有明确地return

您应该返回 Promise.all 承诺并在 then/catch 处理程序中使用结果:

var getAllFeeds = (urls) => {
    var promises = R.map(feedparser.parse, urls);
    return Promise.all(promises)
}

getAllFeeds(URLS)
  .then(items => {
    console.log(items);
  })
  .catch(err => {
    console.error(err);
  })

【讨论】:

  • 看起来我仍然缺少一些东西;什么都不会退出。
  • @KevinWhitaker:也许你被拒绝了
  • 看来我的 REPL 出了问题。 @joews 回答有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 2013-09-11
  • 1970-01-01
相关资源
最近更新 更多