So I've been wondering if I need to wrap things that take time in a process.nextTick
or not.
For example, imagine a function that returns a promise for a xml string translated to an object.
(I've removed require
and other trivialities for readability. You know what's going on here.)
// Return a promise for a javascript object
function parseXml(xml) {
var promise = q.defer();
var parser = new x2je.Parser(function(result, error) {
if (!error)
promise.resolve(result);
else
promise.reject(error);
});
parser.parseString(xml);
return promise.promise;
}
You see some people write functions like so:
// Return a promise for a javascript object
function parseXml(xml) {
var promise = q.defer();
process.nextTick(function(){
var parser = new x2je.Parser(function(result, error) {
if (!error)
promise.resolve(result);
else
promise.reject(error);
});
parser.parseString(xml);
});
return promise.promise;
}
When do I need to use process.nextTick
for best practice coding?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…