最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在将DynamoDb放入node.js lambda之后合并异步http请求功能的问题

IT培训 admin 2浏览 0评论

在将DynamoDb放入node.js lambda之后合并异步http请求功能的问题

我有一个用node.js编写的AWS Lambda函数,该函数将产品发布到DynamoDB表中。除了调用db.put之外,我还需要使用Slack SDK发出一个额外的http请求。这是我希望在DynamoDB调用成功之后运行的示例。我在将其与当前代码合并时遇到问题,因为该示例是异步函数,而我的当前代码未使用异步/等待模式。

我正在尝试调用的代码是用异步语句编写的:

(async () => {
  // See: .postMessage
  const res = await web.chat.postMessage({ channel: conversationId, text: 'Hello there' });

  // `res` contains information about the posted message
  console.log('Message sent: ', res.ts);
})();

下面是我当前的函数createProduct,它将产品发布到DynamoDB(一旦数据库调用成功返回,我想调用上面的代码):

'use strict';
const AWS = require('aws-sdk');
const db = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10' });
const uuid = require('uuid/v4');
const { WebClient } = require('@slack/web-api');
const productsTable = process.env.PRODUCTS_TABLE;
const token = process.env.SLACK_TOKEN;
const web = new WebClient(token);
const conversationId = 'C1232456';


    // Create a response
    function response(statusCode, message) {
      return {
        statusCode: statusCode,
        body: JSON.stringify(message)
      };
    }

    // Create a product
    module.exports.createProduct = (event, context, callback) => {
      const reqBody = JSON.parse(event.body);

      const product = {
        id: uuid(),
        createdAt: new Date().toISOString(),
        userId: 1,
        name: reqBody.name,
        price: reqBody.price
      };

      return db
        .put({
          TableName: productsTable,
          Item: product
        })
        .promise()
        .then(() => {
          callback(null, response(201, product));
        })
        .catch((err) => response(null, response(err.statusCode, err)));
    };
回答如下:

我认为您可以在不等待的情况下在回调之前调用它,并使用Promise.all

callback(null, response(201, product));

[不用等待,就做

const res = web.chat.postMessage({ channel: conversationId, text: 'Hello there' });
Promise.all(res)
  .then(response => {
console.log(response[0])
callback(null, response(201, product));
}); 

在将DynamoDb放入node.js lambda之后合并异步http请求功能的问题

我有一个用node.js编写的AWS Lambda函数,该函数将产品发布到DynamoDB表中。除了调用db.put之外,我还需要使用Slack SDK发出一个额外的http请求。这是我希望在DynamoDB调用成功之后运行的示例。我在将其与当前代码合并时遇到问题,因为该示例是异步函数,而我的当前代码未使用异步/等待模式。

我正在尝试调用的代码是用异步语句编写的:

(async () => {
  // See: .postMessage
  const res = await web.chat.postMessage({ channel: conversationId, text: 'Hello there' });

  // `res` contains information about the posted message
  console.log('Message sent: ', res.ts);
})();

下面是我当前的函数createProduct,它将产品发布到DynamoDB(一旦数据库调用成功返回,我想调用上面的代码):

'use strict';
const AWS = require('aws-sdk');
const db = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10' });
const uuid = require('uuid/v4');
const { WebClient } = require('@slack/web-api');
const productsTable = process.env.PRODUCTS_TABLE;
const token = process.env.SLACK_TOKEN;
const web = new WebClient(token);
const conversationId = 'C1232456';


    // Create a response
    function response(statusCode, message) {
      return {
        statusCode: statusCode,
        body: JSON.stringify(message)
      };
    }

    // Create a product
    module.exports.createProduct = (event, context, callback) => {
      const reqBody = JSON.parse(event.body);

      const product = {
        id: uuid(),
        createdAt: new Date().toISOString(),
        userId: 1,
        name: reqBody.name,
        price: reqBody.price
      };

      return db
        .put({
          TableName: productsTable,
          Item: product
        })
        .promise()
        .then(() => {
          callback(null, response(201, product));
        })
        .catch((err) => response(null, response(err.statusCode, err)));
    };
回答如下:

我认为您可以在不等待的情况下在回调之前调用它,并使用Promise.all

callback(null, response(201, product));

[不用等待,就做

const res = web.chat.postMessage({ channel: conversationId, text: 'Hello there' });
Promise.all(res)
  .then(response => {
console.log(response[0])
callback(null, response(201, product));
}); 
发布评论

评论列表 (0)

  1. 暂无评论