使用 node js 解决和拒绝问题 [英] resolve and reject issue using node js

查看:82
本文介绍了使用 node js 解决和拒绝问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 是否可以通过这种方式将解析或拒绝消息从一个函数返回到另一个函数?

  • Is this possible way to return resolve or reject message from one function to another?

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

As I am writing to pass resolve message in postman whenever my task is completed or reject message when there is some error

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

But after after writing return it still not returning the resolve message or reject message inside Postman

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

any idea how this can be resolve?

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);
      });
}

推荐答案

有几件事我想提一下.

当你创建一个promise时,它应该有resolve()和reject().

When you create a promise, it should have resolve() and reject() inside it.

例如-

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() 来完成 promise 块.然后你可以从一个函数返回那个承诺,在异步函数中使用它或者在它上面应用 .then() 块.

In the method which you have written, You have applied .then() method to non promise object. You have to complete the promise block first with resolve() and reject() inside it. Then you can return that promise from a function, use it in async function Or apply .then() block on it.

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

Also you don't need to add return statement to resolve() and reject() statement. The system will take care of it.

你也可以在 promise 中使用 try catch 块.如果出现任何问题,最好在 catch 块中编写 reject() 语句.

You can also use try catch block inside a promise. Its better to write reject() statement in catch block, if anything goes wrong.

这篇关于使用 node js 解决和拒绝问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆