UnhandledPromiseRejection 未定义问题

UnhandledPromiseRejection undefined problem

希望你能帮助我。我正在开发一种读取一系列数据(数据取自 csv 文件)并检查是否必须将其放入列表中的功能。当我开始检查数据(通过承诺)时,问题就来了,因为它给我一个错误,告诉我没有捕获被拒绝的承诺。您将需要使用以下内容:

//  -npm install email-existence
const emailExistence = require("email-existence");

代码:

function checkEmailExistencePromise(element) {
  return new Promise((resolve, reject) => {
    emailExistence.check(element.email, (error, response) => {
      if (error) {
        reject(error);
        return;
      }
      // If the person has false email the promise will be save (as object) with "Exists" attribute in false.
      if (!response) {
        resolve({
          name: element.name,
          phone: element.phone,
          email: element.email,
          document: element.document,
          weight: element.weight,
          tags: element.tags,
          exists: false,
        });
        return;
      }
      // If the person has valid email the promise will be save (as object) with "Exists" attribute in true.
      resolve({
        name: element.name,
        phone: element.phone,
        email: element.email,
        document: element.document,
        weight: element.weight,
        tags: element.tags,
        exists: true,
      });
    });
  }).catch(() => {
    throw console.error();
  });
}

// I call the function that will write the CSV file with valid email records.
checkEmails();

// This function collects the promises of the "checkEmailExistencePromise" function and dumps them into an array.
async function checkEmails() {
  const promises = sinRepetidos.map((element) =>
    checkEmailExistencePromise(element)
  );

  const values = await Promise.all(promises);

  // Here we go through the promises (which are also objects) and those with the true attribute I put them in a new array that will be printed later.
  values.forEach((element) => {
    if (element.exists === true) {
      checked.push(element);
    }
  });

因为 checkEmailExistencePromise() 可以抛出错误(通过 reject()throw 调用),你需要包装你的

const values = await Promise.all(promises);

也在try..catch中调用checkEmails(),像这样

let values = null;
try {
    values = await Promise.all(promises)
} catch (e) {
    console.error(e) 
}
// do something with values, if it's not null

编辑

因为您很可能不希望 checkEmailExistencePromise 抛出错误,您可以将其替换为:

function checkEmailExistencePromise(element) {
  // NOTE: we're making is so that this promise never rejects - if there's
  // an error in there, we'll assume that the email isn't valid
  return new Promise(resolve => {
    emailExistence.check(element.email, (error, response) => {
      let exists = false;
      if (error) {
        // we can log the error, to make sure we're not doing anything wrong
        // that needs to be fixed - some errors can be legit, though
        console.error(error);
      }
      
      // NOTE: we should probably actually check the response
      if(response) {
        exists = true;
      }
      
      resolve({
        name: element.name,
        phone: element.phone,
        email: element.email,
        document: element.document,
        weight: element.weight,
        tags: element.tags,
        exists
      })
    });
  })
}

我们将任何错误视为电子邮件无效。

此外,如果 element 仅包含这 6 个属性(namephoneemail...),那么您可以进一步简化解析像这样:

resolve(Object.assign({},element,{exists}))

这将创建对象的浅克隆并向其添加 exists 属性