How do I stop the function at the point an error occurred.
I am making an http post call. I have constructed an error-first style callback function.
How do I stop the callback function if an Error object was passed in it? In my use case, I need to stop regardless of error type as I need the data object for subsequent chain of callback functions.
Is it simply return
? I have read what return does on a higher level so hoping to get confirmation if my code is right for what I would like to achieve.
I just want to ensure the proceeding callback functions don't continue as they wont be passed any data objects, therefore, will all be 'errors'
server.js
var request = function(callback) {
// HTTP call to server
},
function (error, response, body) {
if (!error && response.statusCode === 200) {
// processed and created data object
return callback(null, data)
}
else {
console.log(error.stack)
return callback(error) ;
}
})
};
var requestSync = Meteor.wrapAsync(request);
var callback = function(error, data, callback) {
if (error) {
// How do I properly stop this function?
return callback(new Error('custom message I send to client'))
}
else {
// doing something with data object..
}
}
The functions are invoked within my method.
try {
var resultOne = requestSync()
var resultTwo = callback(resultOne)
// more async callback functions will reside here
} catch (e) {
console.error('error occured somehwere = ' + e);
} finally {
//
}
Aucun commentaire:
Enregistrer un commentaire