为什么它不进入函数内部?它只在 AWS Lambda 上输出 "finish" 和 "start"

Why is it not going inside the function? It only output "finish" and "start" on AWS Lambda

我正在 AWS Lambda 上使用 phantom-html-to-pdf 和 NodeJS 执行 html 到 pdf 函数。但是我遇到了一个没有走入函数内部的问题。输出仅显示开始、完成和完成未显示,这意味着它不在函数中。这里有什么问题?

var fs = require('fs')
var conversion = require("phantom-html-to-pdf")();

exports.handler = async (event) => {
    console.log("Start")
conversion({ html: "<h1>Hello World</h1>" }, 
  async(err, pdf) => {
  var output = fs.createWriteStream('output.pdf')
  console.log(pdf.logs);
  console.log(pdf.numberOfPages);
    // since pdf.stream is a node.js stream you can use it
    // to save the pdf to a file (like in this example) or to
    // respond an http request.
  pdf.stream.pipe(output);
  console.log("Done")
});

 console.log("Finish")

};

问题是您已将 lambda 函数标记为异步,这意味着您的函数应该 return 一个承诺。在您的情况下,您没有 return 做出承诺。所以你有两个选择

  • 将转换函数从回调转换为基于承诺。
  • 或者不将函数标记为异步,而是添加回调并执行它。像这样
const fs = require("fs");
const conversion = require("phantom-html-to-pdf")();

exports.handler = (event, context, callback) => {
  console.log("Start");
  conversion({"html": "<h1>Hello World</h1>"},
    // eslint-disable-next-line handle-callback-err
    async (err, pdf) => {
      const output = fs.createWriteStream("output.pdf");
      console.log(pdf.logs);
      console.log(pdf.numberOfPages);
      // since pdf.stream is a node.js stream you can use it
      // to save the pdf to a file (like in this example) or to
      // respond an http request.
      pdf.stream.pipe(output);
      console.log("Done");
      callback(null, "done");
    });
};