Kind of like this?

if (this.on('pipe',function () {
  return true
}){
  //do something if piped 
}else{
  //do something if not piped
}

Because I want to do something different depending on whether or not something is piped to the function or not.

I'm guessing that won't work however because of how async node is, but I need something like that at least.

The main thing is I need something to run either OR but not both, if I were to implement a callback it won't work exactly like I want it to because whatever code I put after the this.on will still run, i need it to not run if this.on fires.

EDIT: Would process.nextTick() be what I need?

有帮助吗?

解决方案 2

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.

其他提示

I can't immagine the real situation where it's needed. I think you try to do something like this:

function mainContextFunction() {
  var isPiped = false;
  this.on('pipe', function(){isPiped = true;});

  function delayedExecution() {
    if (isPiped) ...;
    else ...
  }

  setTimeout(delayedExecution, 1000);
}

mainContextFunction();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top