【问题标题】:Q/Promise handle callback paramsQ/Promise 处理回调参数
【发布时间】:2014-03-12 20:54:32
【问题描述】:
  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)

test().then(over, failed)时,function over不应该执行,程序可以做的很好, 我可以得到结果:

  failed
  wuwuw

但是当我为函数结束添加参数时,函数结束将执行。我会得到:

  failed
  hahaha
  localhost
  wuwuw

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

【问题讨论】:

    标签: node.js promise q


    【解决方案1】:

    但是当我为函数结束添加参数时,函数结束将执行。

    那是因为你在调用它。你还需要传递一个函数给then

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-21
      • 2016-06-08
      • 2015-12-04
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多