使用节点 js 解决和拒绝问题

resolve and reject issue using node js

知道如何解决这个问题吗?

async function readFile(filePath) {}

async function getAllFile(filePath) {
const paths = await readFile(filePath);
}
async function filterFiles(filePath) {
const paths = await getAllFile(filePath);
}

function addDocument(childProduct){
return new Promise((resolve, reject) => {
Document.create({
        name: childProduct,
      },
    }).then(function (filePath) {
        filterFiles(filePath);
        let msg = "Document created Succesfully";
        return resolve(msg);
      })
      .catch(function (err) {
        return reject("Can't be updated please try again :) " + err);
      });
});
}
function updateDoc(data){
return new Promise((resolve, reject) => {
Document.update({
      name: data.name,
      }
      where: {
        product_id: data,
      },
    })
}).then(function (childProduct) {
        addDocument(childProduct);
        let msg = "Updated Successfully";
        return resolve(msg);
      })
      .catch(function (err) {
        return reject("Can't be updated please try again :) " + err);
      });
}
  • Product.findOneDocument.findAll return 一个 Promise,所以它们可以被 returned 直接等待。
  • 您可以将 await func1(); await func2(); await func3() 链接在一个 try{} 块中,并捕获在一个地方发生的任何错误:
const filterFiles = async filePath => {
    const paths = await getAllFiles(filePath);
    // .. Do something else here
    return paths // This is a Promise because async functions always return a Promise
}

const findOneDoc = name => Product.findOne({ where: { name } }); // This func returns a Promise

const findAllDocs = product_id => Document.findAll({ // This func returns a Promise too
    raw: true,
    where: { product_id }
});

(async () => {
    try {
        const childProduct = await findOneDoc("some_name");
        console.log("All good until now!");
        const filePath = await findAllDocs(childProduct._id);
        console.log("Still good");
        const filteredFiles = await filterFiles(filePath);
        console.log("All went well.");
        console.log(filteredFiles);
    } catch (err) {
        // If any of the functions above fails, the try{} block will break and the error will be caught here. 
        console.log(`Error!`, err);
    }
})();

有几件事我想提一下。

当您创建一个承诺时,它应该在其中包含 resolve() 和 reject()。

前任

function testPromise() {
  return new Promise((resolve, reject) => {
    // your logic
    // The followin if-else is not nessesary, its just for an illustration
    if (Success condition met) {
        resolve(object you want to return);
    }else {
        reject(error);
        // you can add error message in this error as well
    }

 });
}
// Calling the method with await
let obj = await testPromise()

// OR call with then, but its better to go with await
testPromise().then((obj)=>{
   // Access obj here
})

在您编写的方法中,您已将 .then() 方法应用于非 promise 对象。您必须首先使用其中的 resolve() 和 reject() 完成 promise 块。然后你可以 return 从一个函数中承诺,在异步函数中使用它或者在它上面应用 .then() 块。

此外,您不需要在 resolve() 和 reject() 语句中添加 return 语句。系统会处理的。

您还可以在 promise 中使用 try catch 块。最好在catch块中写reject()语句,万一出错