Javascript 使用节点js解决并拒绝问题

Javascript 使用节点js解决并拒绝问题,javascript,node.js,promise,callback,nodes,Javascript,Node.js,Promise,Callback,Nodes,这是否可以将解析或拒绝消息从一个函数返回到另一个函数 当我写作时,每当我的任务完成时,我都会在邮递员中传递解析消息,或者在出现错误时拒绝消息 但是在写入return后,它仍然没有返回Postman内部的解析消息或拒绝消息 你知道如何解决这个问题吗 async function readFile(filePath) {} async function getAllFile(filePath) { const paths = await readFile(filePath); } Pr

这是否可以将解析或拒绝消息从一个函数返回到另一个函数

  • 当我写作时,每当我的任务完成时,我都会在邮递员中传递解析消息,或者在出现错误时拒绝消息

  • 但是在写入
    return
    后,它仍然没有返回Postman内部的解析消息或拒绝消息

  • 你知道如何解决这个问题吗

    async function readFile(filePath) {}
    
    async function getAllFile(filePath) {
    const paths = await readFile(filePath);
    }
    
    • Product.findOne
      Document.findAll
      返回承诺,这样就可以直接返回并等待它们
    • 您可以链接
      等待func1();等待函数2();在一个
      块中等待func3()
      尝试{}
      并捕获在一个位置发生的任何错误:
    • Product.findOne
      Document.findAll
      返回承诺,这样就可以直接返回并等待它们
    • 您可以链接
      等待func1();等待函数2();在一个
      块中等待func3()
      尝试{}
      并捕获在一个位置发生的任何错误:

    有几件事我想提一下

    创建承诺时,承诺中应该包含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()方法应用于非承诺对象。您必须首先使用resolve()和reject()完成承诺块。然后,您可以从函数返回该承诺,在异步函数中使用它,或者在其上应用.Then()块

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


    您还可以在承诺中使用try-catch块。如果出现任何问题,最好在catch块中编写reject()语句。

    我想提及的事情很少

    创建承诺时,承诺中应该包含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()方法应用于非承诺对象。您必须首先使用resolve()和reject()完成承诺块。然后,您可以从函数返回该承诺,在异步函数中使用它,或者在其上应用.Then()块

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



    您还可以在承诺中使用try-catch块。如果出现任何错误,最好在catch块中编写reject()语句。

    这里有几处错误。首先出现语法错误
    Promise()…然后()
    (double
    )。您正在创建的承诺永远不会解决,也永远不会做任何事情,因此
    .then()
    永远不会发生。那怎么办
    .then(function(childProduct){
    ?在这种情况下,既然承诺是空的并且永远不会解决,那么什么是
    childProduct
    ?完全去掉承诺,只使用
    async
    函数。
    async
    函数总是返回承诺;如果编写
    则返回“test”
    ,返回的类型是
    Promise
    ,而不是
    。Hello@JeremyThille我已经更新了问题,希望现在可以理解,请看一下:)事实上,一大块代码丢失了,现在更清楚了。我已经发布了一个答案。但是我不理解你的函数命名。
    addDocument
    找到一个文档?
    updateDoc
    找到所有文档?这个命名没有意义!Hi@JeremyThille因为我错误地在我的函数中写下了findAll findOne代码,我有更新代码这里有几件事是错误的。首先你有一个语法错误
    Promise()…然后()
    (double
    )。你创建的承诺永远不会解决,也不会做任何事情,所以
    。然后()
    永远不会发生。然后呢?
    。然后(函数(childProduct){
    ?在这种情况下,既然承诺是空的并且永远不会解析,那么什么是
    childProduct
    ?完全去掉承诺,只使用
    async
    函数。
    async
    函数总是返回承诺;如果编写
    函数,则返回“test”
    ,返回的类型是
    Promise
    ,而不是
    。Hello@JeremyThille我已经更新了问题,希望现在可以理解,请看一下:)事实上,一大块代码丢失了,现在更清楚了。我已经发布了一个答案。但是我不理解你的函数命名。
    addDocument
    找到一个文档?
    updateDoc
    找到所有文档?这个命名没有意义!Hi@JeremyThille因为我错误地在我的函数中写下了findAll findOne代码,我有更新代码Hi@jeremy我如何显示函数中的reject resolve msg?因为我在这里只写了两个promise函数,但在我的代码中有9个promise函数,我想在我的postmanWell上显示reject resolve msg。当然,在
    catch
    块中。我告诉过你,所有错误都会在一个位置捕获。不需要捕获9个错误在9个不同的地方。不是最后一行代码
    console.log('Error!',err)
    ?解析消息呢?每个函数都有不同的消息,这就是为什么:)答案更新。Hello@Jeremy在我上面的问题中,我从我的文档中编写
    findAll、findOne或create
    时,会得到object
    product
    ,通过这个对象,我可以获取ID,作为其他数据的外键,这就是我需要上述方法的原因d如果我使用你的方法,那么我将如何获取objectHi@jeremy我如何显示函数中的reject resolve msg?因为我在这里只编写了两个promise函数,但在我的代码中有9个promise函数,我想在我的postmanWell上显示reject resolve msg,当然是在
    catch
    块中。我告诉过你,所有错误都会被捕捉到e位置。不需要在9个不同的位置捕获9个错误。不是最后一行代码
    console.log('Error!',err)
    ?那么resolve msg呢?因为每个函数都有不同的msg,这就是为什么:)Answer更新的原因。您好@
    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);
        }
    })();
    
    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
    })