为什么在.then()链接到Promise时未定义值?

时间:2017-06-08 15:15:12

标签: javascript promise

鉴于



function doStuff(n /* `n` is expected to be a positive number */) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      resolve(n * 10)
    }, Math.floor(Math.random() * 1000))
  })
  .then(function(result) {
    if (result > 100) {
      console.log(result + " is greater than 100")
    } else {
      console.log(result + " is not greater than 100");
    }
  })
}

doStuff(9)
.then(function(data) {
  console.log(data) // `undefined`,  why?
})




为什么data undefined .then()doStuff()有联系?

6 个答案:

答案 0 :(得分:10)

因为从Promise链接到return构造函数的.then()或其他值不是Promise

请注意,.then()会返回一个新的Promise对象。

解决方案是return return一个值或其他函数调用Promise是一个值或.then()来自function doStuff(n /* `n` is expected to be a positive number */) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(n * 10) }, Math.floor(Math.random() * 1000)) }) .then(function(result) { if (result > 100) { console.log(result + " is greater than 100") } else { console.log(result + " is not greater than 100"); } // `return` `result` or other value here // to avoid `undefined` at chained `.then()` return result }) } doStuff(9) .then(function(data) { console.log("data is: " + data) // `data` is not `undefined` });



UPDATE Mytable SET MyField=Cleanup(MyField)




答案 1 :(得分:4)

问题正面临:

return 
(new Promise(..)) //the promise we want to return
.then(()=>undefined) // the promise were actually returning, which resolves to undefined

正如您可能已经注意到的那样,然后返回一个新的承诺。这有一个很好的理由,它使得链接容易,例如:

getUser()//an asynchronous action
 .then(user=>login(user))//then if we get the user,promise to log in
 .then(token=>console.log("logged in,token is "+token) //then if we logged in, log it
 .catch(error=>"login failed");//catch all errors from above

但这也创造了我们面临的小陷阱。解决方案可能是返回原始承诺而不是 .then()自动返回的新承诺,因为它被解析为未定义为函数在里面并没有明确地返回一些东西:

//what were doing:
Promise.resolve(n*10)//the original promise resolves to n*10
.then(a=>undefined)//the then gets n*10 passed as a, but returns undefined
.then(b=>console.log(b));//b will be undefined  :0 

//what we want:
var promise=Promise.resolve(n*10);
promise.then(a=>undefined);//a is n*10, this resolves to undefined
promise.then(b=>console.log(b));//but this still logs n*10, as its the original promise  :) 

正如你所看到的,为了返回原始的promise,我们只是将它存储在一个变量中,然后为它分配一个.then处理程序,并且仍然可以引用我们可以为其他处理程序分配的原始promise(或者返回)。

行动中:

function doStuff(n /* `n` is expected to be a number */) {

    //create a new promise and store it

    var promise=new Promise(function(resolve, reject) {
        setTimeout(function() {
           resolve(n * 10)
        },1000);
    });

    //add a then handler to this promise  

    promise.then(result=>console.log(result + " is "+result<100?"not":""+" greater than 100"));

    //return the original one

    return promise;

}

doStuff(9).then(function(data) {
  console.log(data) //not undefined, as original promise
})

答案 2 :(得分:3)

doStuff正在返回Promise。但是,您的上一个then函数未返回任何值,因此data将显示为undefined

在promises中,下一个then函数的参数值是前一个then函数的返回值。

&#13;
&#13;
function doStuff(n /* `n` is expected to be a positive number */) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      resolve(n * 10)
    }, Math.floor(Math.random() * 1000))
  })
  .then(function(result) {
    if (result > 100) {
      console.log(result + " is greater than 100")
    } else {
      console.log(result + " is not greater than 100");
    }
    return result;
  })
}

doStuff(9)
.then(function(data) {
  console.log(data) // `90`
})
&#13;
&#13;
&#13;

答案 3 :(得分:1)

您没有将.then()链接到Promise的结果返回。你需要添加返回结果;到.then()

答案 4 :(得分:1)

由于您的数据值是上一个.then()的返回值,因此您的上一个.then()没有有效的返回值。

因此,您可以在最后.then()函数中添加返回值。

答案 5 :(得分:1)

您必须从链接到Promise的.then()返回结果。 就您而言,只需添加

return result;

到第一个.then()