在 Node 中,如何使用承诺从多个 URL 请求 JSON?

In Node, how do I request JSON from multiple URLs using promises?

请原谅这个相当具体的问题,尽管我认为一般的最终目标可能对其他人有用。

目标:使用从多个 JSON API URL 请求的数据填充 MongoDB。

简短的问题:到目前为止,我在使用 Bluebird 的 request-promise 上取得了一些成功:

var rp = require('request-promise');
var options = {
    uri: 'http://www.bbc.co.uk/programmes/b006qsq5.json',
    headers: {
        'User-Agent': 'Request-Promise'
    },
    json: true
};

rp(options)
    .then(function (body) {
        // Mongoose allows us query db for existing PID and upsert
        var query = {pid: body.programme.pid},
            update = {
                name: body.programme.title,
                pid: body.programme.pid,
                desc: body.programme.short_synopsis
            },
            options = { upsert: true, new: true };

        // Find the document
        Programme.findOneAndUpdate(query, update, options, function(err, result) {
            if (err) return res.send(500, { error: err });
            return res.send("succesfully saved");
        });
    })
    .catch(function (err) {
        return res.send(err);
    })

但是,如果任何 promise 被拒绝,我该如何循环访问 URL 数组而不导致程序失败? 例如,如果任何 URL 错误,使用 Bluebird 就会失败。

const urls = ['http://google.be', 'http://google.uk']

Promise.map(urls, rp)
  .map((htmlOnePage, index) => {
    return htmlOnePage;
  })
  .then(console.log)
  .catch((e) => console.log('We encountered an error' + e));

因为我想用成功的请求写入数据库,并忽略那些可能没有立即响应的请求,所以我需要一些可以跳过被拒绝的承诺的东西,.all 做不到。

长问题: 我整天都在阅读有关承诺的内容,这让我头疼!但是我找到了一些很好的资源,例如 https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html,其中提到了 Promise 工厂的使用。这对我的情况有用吗?我最初认为我应该发出每个请求,处理结果并将其添加到数据库,然后继续下一个请求;但是看过 .all 我想我应该做所有的请求,将结果保存在一个数组中,然后用我的数据库保存函数循环它。

我什至应该为此使用 Promises 吗?也许我应该使用 async.js 和 运行 之类的东西来串联我的请求。

非常感谢任何帮助或想法。

我只是使用请求并编写我自己的 promise,里面有 try catch,只能解析。下面的伪示例

var request = require('request')

var urls = ['http://sample1.com/json', 'http://sample2.com/json']

var processUrl = (url) => { 
   return new Promise((resolve,reject)=> {
     var result;
     try {
        var myRequest = {
           uri: url,
           method: 'GET',
           header: {...}
        };
        request(option, (res,body,err)=> {
            if(err) {
               result = err;
               return;
            }
            result = body;
        })
     }
     catch(e) {
         result = e;
     }
     finally {
        resolve(result)
     }
  })
}

我认为你的问题不是蓝鸟 api 而是构建你的承诺链。

const reducePropsToRequests = (props) => Promise.resolve(Object
  .keys(props)
  .reduce((acc, key) => {
    acc[key] = request(sources[key]);
    return acc;
  }, {}));

const hashToCollection = (hash) => Promise.resolve(Object
  .keys(hash)
  .reduce((acc, k) => {
    return [...acc, {source: k, data: hash[k]}];
  }, []));

const fetchFromSources = (sources) => Promise.props(sources);

const findSeveralAndUpdate = (results) => Promise
  .each(results.map(obj => {
    // you have access to original {a: 'site.com'}
    // here, so use that 'a' prop to your advantage by abstracting out
    // your db config somewhere outside your service
    return Programme.findOneAndUpdate(someConfig[obj.source], obj.data);
  }))

const requestFromSeveralAndUpdate = (sources) => reducePropsToRequests(sources)
  .then(fetchFromSources)
  .then(hashToCollection)
  .then(findSeveralAndUpdate)
  .catch(/* some err handler */);

requestFromSeveralAndUpdate({ a: 'site.com', b: 'site.net' });

我不知道这是否适合你的情况,但我认为你可以使用计数器来检查所有承诺何时返回,而不管每个承诺是否已被解决或拒绝

var heroes = [
  'Superman',
  'Batman',
  'Spiderman',
  'Capitan America',
  'Ironman',
];

function getHero(hero) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      return Math.round(Math.random()) ? resolve(hero + ' lives') : reject(hero + ' dead');
    }, Math.random() * 3000)    
  })
}

function checkHeroes() {
  var checked = heroes.length;
  heroes.forEach((hero) => {
    getHero(hero)
    .then((res) => {
      checked --;
      console.log(res);
      if (!checked) done();   
    })
    .catch((err) => { 
      checked --;
      console.log(err);
      if (!checked) done();      
    });           
  }) 
}

function done() {
  console.log('All heroes checked');
}

checkHeroes();

But how do I loop over an array of URLs, without the program failing if any of the promises are rejected?

如果您 return 来自 .catch 的值不是被拒绝的承诺,您将 return 一个已解决的承诺

因此,对于每个单独的请求,您的 .then 可以 return 一个像

这样的对象
{
    success: true,
    result: whateverTheResultIs
}

和你的收获 returns

{
    success: false,
    error: whateverTheErrorIs
}

真的,你不需要成功 属性,不过这很方便

因此代码将是 - 假设 process(url) return 是一个 Promise

Promise.map(urls, url => 
    process(url)
    .then(result => ({result, success:true}))
    .catch(error => ({error, success:false}))
)
.then(results => {
    let succeeded = results.filter(result => result.success).map(result => result.result);
    let failed = results.filter(result => !result.success).map(result => result.error);
});

或者,在 ES5 中

Promise.map(urls, function (url) {
    return process(url).then(function (result) {
        return { result: result, success: true };
    }).catch(function (error) {
        return { error: error, success: false };
    });
}).then(function (results) {
    var succeeded = results.filter(function (result) {
        return result.success;
    }).map(function (result) {
        return result.result;
    });
    var failed = results.filter(function (result) {
        return !result.success;
    }).map(function (result) {
        return result.error;
    });
});