I'm using Node.js and q library.
I have a code that looks like this:
checkIfThingExists(function(idForAThing){
if(idForAThing){
updateThingData(idForAThing);
} else {
createThing(function(idForAThing){
updateThingData(idForAThing);
});
}
})
As you can see I need to call updateThingData(); twice.
Is there a way I can use promises to call updateThingData()
just once, for example something like this? Of course this does not work and idForAThing
is always undefined
when the if statement runs:
checkIfThingExists(function(idForAThing){
if(!idForAThing){
createThing().then(function(newIdForAThing){
idForAThing = newIdForAThing
})
}
updateThingData(idForAThing);
})