Ok, what I needed to do was Defer it to the next stack process with process.nextTick()
so that it executed AFTER the .on callback was executed. so it's like this:
upload = function (path,file,callback){
this.notpiped = true
this.on('pipe',function () {
this.notpiped = false
})
process.nextTick(function() {
if(this.notpipe){
cback(path,file,callback)
}
})
this.end = function () {
//handle piped data, eg, parse it, or set it or whatever and then use cback
}
}
upload.prototype._write = (chunk, encoding, callback){
this.data = this.data + chunk
}
so now this .end
will fire like normal ONLY if .pipe
gets called on it BUT if pipe doesn't get called then .end
will never fire so it's ok to do it like this.
I can now have a function that can have data streamed to it, OR directly used depending on circumstances.
My actual function I'm using does a bit more then this and looks a little different but the concept is the same.