在AWS Lambda中处理多个承诺

时间:2018-06-28 04:00:19

标签: node.js amazon-web-services aws-lambda

我正在尝试使用lambda函数通过其API获取黑客新闻报道。如果我将其创建为常规的Nodejs cron作业,则该方法似乎可行,但是作为lambda函数,它仅以成功调用结束而没有任何输出。下面是我的代码

const PromisePool = require("es6-promise-pool");
const rp = require("request-promise");

const getTopStories = async () => {
  let options = {
    method: "GET",
    uri: "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty",
    headers: {
      'Content-Type': 'application/json'
    },
    json: true
  };
    return rp(options).then(stories => {
      return (stories);
    })
    .catch(err => err);
}

const getStoryDetails = (storyId) => {
  let url = "https://hacker-news.firebaseio.com/v0/item/"+storyId+".json?print=pretty";
  let options = {
    method: "GET",
    uri: url,
    headers: {
      'Content-Type': 'application/json'
    },
    json: true
  };
  return new Promise((resolve, reject) => {
    rp(options)
    .then(response => {
      if(response.type === "story"){
        resolve({
          story_id: response.id,
          pubDate: response.time,
          title: response.title,
          url: response.url,
          provider: "Hacker News",
          type: response.type,
          status: "NEW"
        });
      }
    })
    .catch(err => reject(err));
  });
}


let stories = [];

let promiseProducer = async (sList) => {
    stories = await sList.map(id => getStoryDetails(id));
    return stories;
}

exports.hknFeed = async (event) => {
    let idList = await getTopStories();
    console.log(idList);
    let pool = new PromisePool(promiseProducer(idList), 4);
    let poolPromise = pool.start();
    poolPromise.then(() => {
        Promise.all(stories).then(values => {
            console.log(values);
        })
    })
    return idList;
};

有人可以指出我想让它正常工作的什么原因。

注意:getTopStories返回一个由500个值组成的数组,这将引发过多的请求,并且我认为这会导致某处的套接字错误。但是,如果我们将返回的数组限制为10-15个值的数组,那么上面的代码就可以工作了,并且Promise.all会解析每个promise并将其输出到控制台。

1 个答案:

答案 0 :(得分:1)

答案: 道歉...好像我没有注意。上面的代码现在可以在lambda函数中运行,因为我的return语句不合适。它应该在Promise.all()。then(()=> {return})中。像下面一样

const PromisePool = require("es6-promise-pool");
const rp = require("request-promise");

const getTopStories = async () => {
  let options = {
    method: "GET",
    uri: "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty",
    headers: {
      'Content-Type': 'application/json'
    },
    json: true
  };
    return rp(options).then(stories => {
      return (stories);
    })
    .catch(err => err);
}

const getStoryDetails = (storyId) => {
  let url = "https://hacker-news.firebaseio.com/v0/item/"+storyId+".json?print=pretty";
  let options = {
    method: "GET",
    uri: url,
    headers: {
      'Content-Type': 'application/json'
    },
    json: true
  };
  return new Promise((resolve, reject) => {
    rp(options)
    .then(response => {
      if(response.type === "story"){
        resolve({
          story_id: response.id,
          pubDate: response.time,
          title: response.title,
          url: response.url,
          provider: "Hacker News",
          type: response.type,
          status: "NEW"
        });
      }
    })
    .catch(err => reject(err));
  });
}


let stories = [];

let promiseProducer = async (sList) => {
    stories = await sList.map(id => getStoryDetails(id));
    return stories;
}

exports.hknFeed = async (event) => {
    let idList = await getTopStories();
    console.log(idList);
    let pool = new PromisePool(promiseProducer(idList), 4);
    let poolPromise = pool.start();
    poolPromise.then(() => {
        Promise.all(stories).then(values => {
            console.log(values);
            return values;
        })
    })
};