0

I'm trying to insert a new document based on the request received and here's what I'm trying to do.

var function = (req,callBack)=>{
    queryDB.findOne({query}).then((result)=>{
        return(result); // see if requested object exists in DB and return if yes  
}).then((result)=>{
    if(condition:if required object exists){
        updateDB.updateOne({query}).then((result)=>{ //update required object with new values
            if(condition:update successful){
                ref.function(value).then((k)=>{ // invoke for computation
                    return(k);
                }).then((k)=>{
                    var document = new Document({ //create a new document 
                    ....
                    ....
                    ....
                    ....
                    ....
                    });

                    document.save().then((doc)={
                        callBack(doc); //return new document after successful insert
                    }).catch((err)=>{
                        updateDB.updateOne({query}).then((doc)=>{}) //revert back the update done earlier incase of error while inserting new document
                        callBack(err);
                    });
                }).catch((error)=>{
                    updateDB.updateOne({query}).then((doc)=>{}) //revert back the update done earlier incase of error when invoked function has an error (2)
                    callBack(err);
                })
            }
        }).catch((errorMessage)={
            callBack(errorMessage);
        })
    }else{
        callBack("Required Object Doesn't Exist");
    }
}).catch((errorMessage)=>{
    callBack(errorMessage);
})

}

The behavior I'm expecting is if there is an error in ref.function it should revert the update and send back error message but the API hangs indefinitely without reverting back the update done prior.

While the revert back works fine in-case of error in inserting new document to DB.

David
  • 15
  • 7
  • Do not use a `callBack`, return a promise instead. Try promise chaining to return a promise for the innermost result. – Bergi Nov 13 '18 at 21:26
  • What exactly is `ref.function`, and what is the error in it that causes this behaviour? – Bergi Nov 13 '18 at 21:40
  • @Bergi this function generates a unique ID based on certain criteria. this is a common to all API in the application I've deleted the file ref that has this function, simulating a case which might happen files being deleted/renamed and not updated. – David Nov 14 '18 at 17:18
  • What do you mean by "*I've deleted the file that has this function*"? If the function you try to call does not exists, that's an exception you cannot catch with promise `.catch()`, you will need a `try` block for that. – Bergi Nov 14 '18 at 17:34

1 Answers1

0

Since ref.function(value) is followed by a then with return k, the catch block probably only catches the errors if ref.function rejects the promise and the errors in the block below (after ref.function)

.then((k)=>{ // invoke for computation
   return(k);
})

Try adding Promise.resolve before the ref.function

 return Promise.resolve(
 () => {
   return ref.function(value);
 }).then(
 k => {
    ...
 }).catch(
 err => {
    updateDB.updateOne({query}).then((doc)=>{}) //revert back the update done earlier incase of error when invoked function has an error (2)
    callBack(err);
 });
William Juan
  • 1,395
  • 1
  • 7
  • 10
  • Why? Please explain what difference it would make. – Bergi Nov 13 '18 at 21:25
  • @Bergi Since the `ref.function` is the first call, I think that the `catch` will only catch the errors that happens after that call. With the `success, error`, it should catch the error from your `ref.function`. – William Juan Nov 13 '18 at 21:28
  • No change still keeps hanging. – David Nov 13 '18 at 21:30
  • @WilliamJuan Yeah, [I know the difference](https://stackoverflow.com/q/24662289/1048572), but please put that explanation in your answer and why you think it caused the problems for the OP – Bergi Nov 13 '18 at 21:30
  • @WilliamJuan Ah, now I see what you mean, but no that's wrong. `catch` can catch errors from anywhere as long that leads to the promise getting rejected. [`.then((k)=>{ return(k); })` is a no-op](https://stackoverflow.com/q/41089122/1048572), but it still forwards rejections. – Bergi Nov 13 '18 at 21:37