Domanda

  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)

When test().then(over, failed), function over should not execute,The program can do well, I can get the result:

  failed
  wuwuw

But when I add params for function over, the function over will execute. I will get:

  failed
  hahaha
  localhost
  wuwuw

Obviously, I don't want the function over execute, but I need it get the params when test() resolved.

È stato utile?

Soluzione

But when I add params for function over, the function over will execute.

That's because you're calling it. You still need to pass a function to then:

test().then(function(testResult) {
    over("localhost");
}, failed)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top