Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我如何重定向基于";的流;“内部状态”;_Javascript_Node.js_Stream_Gulp - Fatal编程技术网

Javascript 我如何重定向基于";的流;“内部状态”;

Javascript 我如何重定向基于";的流;“内部状态”;,javascript,node.js,stream,gulp,Javascript,Node.js,Stream,Gulp,我正在为gulp编写一个插件,该插件使用web服务,并根据响应执行一项或多项操作。算法是这样的: stream1 = through.obj(function(src, enc, cb) { if src.is_a_buffer() http_request(options) http_request.on('response', function () { if (statusCode = 200) { /* Normal cour

我正在为gulp编写一个插件,该插件使用web服务,并根据响应执行一项或多项操作。算法是这样的:

stream1 = through.obj(function(src, enc, cb) {
 if src.is_a_buffer()
     http_request(options)
     http_request.on('response', function () {
        if (statusCode = 200) {
            /* Normal course, everything works fine here */
            do_something()
            return cb()
        } else {
            /* Exception course, although the stream2 is created, is never executed */
            stream1.pipe(stream2())
        }

}, function (cb) {
    cb()
});

stream2 = through.obj(function(src,enc,cb) {
     do_other_stuff()
     stream2.push(src)
     return cb()
}, function (cb) {
    cb()
});
当我运行代码stream2时,它从不例外。
由于我是节点流的新手,我想我误解了什么。你们中有谁能帮我理解我这里出了什么问题吗?

当你调用
stream1.pipe(stream2())
时,
stream1
已经发出了数据(可能是全部);进行该调用不会将执行传递到
stream2
。根据您的需要,有几种方法可以解决此问题:

注意:我只是在这里修改原始伪代码

选项1:

不必费心处理
stream2
,直接调用
do\u other\u stuff()

stream1 = through.obj(function(src, enc, cb) {
 if src.is_a_buffer()
     http_request(options)
     http_request.on('response', function () {
        if (statusCode = 200) {
            /* Normal course, everything works fine here */
            do_something()
            cb()
        } else {
            do_other_stuff()
            cb()
        }

}, function (cb) {
    cb()
});
选项2:

如果出于其他目的需要
stream2
,请将
through.obj()
回调拉入到它自己的可调用函数中,并直接从else子句调用它

stream1 = through.obj(function(src, enc, cb) {
 if src.is_a_buffer()
     http_request(options)
     http_request.on('response', function () {
        if (statusCode = 200) {
            /* Normal course, everything works fine here */
            do_something()
            return cb()
        } else {
            processStream2(src, enc, cb)
        }

}, function (cb) {
    cb()
});

function processStream2(src, enc, cb) {
     do_other_stuff()
     return cb()
}

stream2 = through.obj(processStream2, function (cb) {
    cb()
});

我希望这会有所帮助:)

这应该是普通的javascript还是一种可以转换为javascript的语言?如果是前者,那么右值为
return
的赋值是无效的javascript…@mscdex my bad。第二个选项完全适合我的场景,现在看起来很简单:D。我只需要一个可以在流之间共享的转换函数。非常感谢。伟大的我很高兴能帮上忙:)