Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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
Node.js Q/Promise句柄回调参数_Node.js_Promise_Q - Fatal编程技术网

Node.js Q/Promise句柄回调参数

Node.js Q/Promise句柄回调参数,node.js,promise,q,Node.js,Promise,Q,当test()。然后(结束,失败),函数结束不应执行,程序可以运行良好, 我可以得到以下结果: var Q = require("q"); function test(v){ var deferred = Q.defer() if (v) { console.log("success"); deferred.resolve(); } else{ console.log("failed"); deferred.

test()。然后(结束,失败)
,函数结束不应执行,程序可以运行良好, 我可以得到以下结果:

  var Q = require("q");
  function test(v){
    var deferred = Q.defer()
    if (v) {
      console.log("success");
      deferred.resolve();
    }
    else{
      console.log("failed");
      deferred.reject(new Error("failed"))
    } 
    return deferred.promise;
  }

  var over = function(url){
    console.log("hahaha")
    console.log(url)
  }

  var failed = function(){
    console.log("wuwuw")
  }

  test().then(over, failed)

  test().then(over("localhost"), failed)
但当我为函数over添加参数时,函数over将执行。我会得到:

  failed
  wuwuw
显然,我不希望函数过度执行,但我需要它在解析test()时获取参数

但当我为函数over添加参数时,函数over将执行

那是因为你在叫它。您仍然需要将函数传递给
,然后

  failed
  hahaha
  localhost
  wuwuw
test().then(function(testResult) {
    over("localhost");
}, failed)