Possible Duplicate:
Is it possible to catch exceptions thrown in a JavaScript async callback?
I am pulling orders from a database and then requesting, I want to be able to break the forEach
and I can do that with a try
and catch
but I think that the callback isn't working
try{
orders.find().forEach(function(order){
request({
"method":"get",
"json":true
},function (error, response, data){
if (!error && response.statusCode == 200) {
console.log("Order id "+order.id);
}else{
throw "limit";
}
});
});
}catch(err){
if(err == "limit"){
console.log("error occured");
}
}
This is what I'm getting
/Users/thomas/Desktop/forerunner/retrieveTransactions.js:120
throw "limit";
^
limit
I just want:
error occured
You need to add try-catch inside the callback function, something like:
orders.find().forEach(function(order){
request({
"method":"get",
"json":truetry{
},function (error, response, data){
try{
if (!error && response.statusCode == 200) {
console.log("Order id "+order.id);
}else{
throw "limit";
}
}catch(err){
if(err == "limit"){
console.log("error occured");
}
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With