return true or false from a function that is inside another function in nodejs

1

This function is to detect if there were no errors at the time of deleting the records but I call it from another function as well

if(!deleteAllSectionsPage(123) )
{
  //alguna accion.
}

I have

function deleteAllSectionsPage(idpage)
{
    Section.destroy(
        {
            where : { 
                page_id: idpage,
                status : 1
            }
        }
    ).then(section => {
        return  true 
    }).catch(function (err) {
        return false 
    });

}

but I do not return the Boolean value that is in

).then(section => {
        return  true 
    }).catch(function (err) {
        return false 
    });

I do not know the truth because, I need knowledge in this type of situations, I have been looking for but I can not find it and I am lost in this. Can someone tell me how I can solve this?

    
asked by santiago 02.04.2018 в 05:29
source

1 answer

3

Well I think what you could do, is to handle it in another way, because you will need to return a Promise or Promise.

You need to modify the function to return the Promise:

function deleteAllSectionsPage(idpage)
{
    return Section.destroy(
       {
           where : { 
               page_id: idpage,
               status : 1
           }
       }
    ).then(section => {
        return  true 
    }).catch(function (err) {
        return false 
    });

}

then the if you would have to handle it in another way:

deleteAllSectionsPage(1).then(function(result) {
     if (result) {
       // Paso
     } else {
       // No Paso    
     }
})
  

For reasons that the query to the server or the http query may take some time you have to wait for this action to finish to know what result you get from the function.

Here the problem is that it is dealing with Asynchronous Actions, because when doing an http query, javascript processes this action in parallel and continues executing the code and in these ways asynchronous processes are formed, improving the efficiency in execution. I recommend you read more of the Promises. You can start here: link

I hope this helps you.

    
answered by 02.04.2018 / 05:52
source