【问题标题】:Nodejs Lambda, what the purpose of .promise()?Nodejs Lambda,.promise() 的目的是什么?
【发布时间】:2019-12-19 08:56:45
【问题描述】:

promise() 在此处使用 AWS lambda。

我想在文件存储到 s3 后立即发送信号。我想知道 .promise() 在这里做了什么。 (--s3.putObject({}).promise()--)

fetch(send_to_this_url) 的时间戳在 s3 存储上看到文件时几乎相同。基于此,promise() 不会异步操作。我这里有什么问题吗?

在前几行中,响应正常时返回响应。但是,如果它早点返回,文件就不会存储在 s3 中,但它已经存储了,所以我想知道第一个 if(response.ok) 的目的是什么。

请与我分享任何想法。!


fetch(url)
  .then((response) => {
    if (response.ok) {
      return response;
    }
    return Promise.reject(new Error(
          `Failed to fetch ${response.url}: ${response.status} ${response.statusText}`));
  })
  .then(response => response.buffer())
  .then((buffer) => {

      s3.putObject({
        ACL: "public-read",
        Bucket: process.env.BUCKET,
        Key: key,
        Body: buffer
      }).promise();   

      try{
        const send_to_this_url = "someurl?key=" + key;
        fetch(send_to_this_url);
      }catch(e){
        callback('error' + e.message);
      }

    }
  )
  .then(v => callback(null, v), callback);

【问题讨论】:

    标签: javascript node.js lambda promise aws-sdk-js


    【解决方案1】:

    AWS JavaScript SDK 代码中的.promise() 表示一种将基于回调的 API 转换为返回承诺的方法。

    这让你可以在上面await,或者像任何其他承诺一样调用.then

    您可以通过链接从s3.putObject().promise() 返回的承诺在您的代码中利用这一点,就像您对所有其他人所做的那样:

    fetch(url)
    .then((response) => {
      // ...
    })
    .then(response => response.buffer())
    // make this part of the promise chain
    // use the promise returned when putting
    // an object to s3
    .then((buffer) => s3.putObject({
        ACL: "public-read",
        Bucket: process.env.BUCKET,
        Key: key,
        Body: buffer
      }).promise()
    )
    .then(() => {
      // this will run if all promises before it in the chain have resolved successfuly
      const send_to_this_url = "someurl?key=" + key;
      return fetch(send_to_this_url);
    })
    .then(resultFromFetchSendToThisUrl => {
      // ...
    })
    .catch(() => {
      // this will run if any promise in the chain above rejects
      // including the one from s3.putObject
    })
    

    也许使用 async-await 会使代码更具可读性:

    const response = await fetch(url);
    
    if (!response.ok) {
      throw new Error(
        `Failed to fetch ${response.url}: ${response.status} ${response.statusText}`
      )
    }
    
    const buffer = await response.buffer();
    
    const result = await s3.putObject({
      ACL: "public-read",
      Bucket: process.env.BUCKET,
      Key: key,
      Body: buffer
    }).promise()
    
    const send_to_this_url = "someurl?key=" + key;
    const resultFromFetchSendToThisUrl = await fetch(send_to_this_url);
    

    【讨论】:

    • 你的解释很清楚。太感谢了。 @nem035
    • 很高兴为您提供帮助:)
    【解决方案2】:

    每个人:https://aws.amazon.com/blogs/developer/support-for-promises-in-the-sdk/

    AWS.Request.promise() 方法不使用回调,而是提供了一种调用服务操作并返回管理异步流而不是回调的承诺的方法。

    因此,AWS SDK 中的 .promise() 方法会返回一个承诺,这听起来就像在现实生活中一样,是一个返回某些东西的承诺。那么让我们看看你的代码:

    s3.putObject({
      ACL: "public-read",
      Bucket: process.env.BUCKET,
      Key: key,
      Body: buffer
    }).promise();   
    

    这和写在回调中是一样的

    s3.putObject({
      ACL: "public-read",
      Bucket: process.env.BUCKET,
      Key: key,
      Body: buffer
    }, function(e, data) {
      if (e) {
        callback('error' + e.message);
      } else {
         // do your thing here.
         return fetch("someurl?key=" + data.key;);
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      相关资源
      最近更新 更多