简体   繁体   English

Promise.then()之后的Promise.all()返回未定义的值

[英]Promise.all() after Promise.then() returning undefined values

I have the following code 我有以下代码

var pOne = new Promise(function(callback){
    setTimeout(function(){
        callback(false);
    }, 100);
}).then(function(v){
    console.log("pOne: " + v);
});

var pTwo = new Promise(function(callback){
    setTimeout(function(){
        callback(true);
    }, 100);
}).then(function(v){
    console.log("pTwo: " + v);
});

Promise.all([pOne, pTwo]).then(function(values){
    console.log(values);
});

The console.log(values) displays [undefined, undefined] in the console. console.log(values)在控制台中显示[undefined, undefined] My understanding of promises is that I should be able to chain the then() method. 我对promise的理解是,我应该能够链接then()方法。 Does chaining not work with the Promise.all() or is this a bug. 链接是否不适用于Promise.all()或这是一个错误。

Note: I am using the promise-polyfill but running it on chrome, so it technically it is using native chrome implementation of promises. 注意:我使用的是promise-polyfill,但是在chrome上运行,因此从技术上讲,它是使用promise的本机chrome实现。

your pOne and pTwo promises don't return anything. 您的pOne和pTwo承诺不会返回任何内容。

Try this: 尝试这个:

var pOne = new Promise(function(callback){
    setTimeout(callback, 100, true);
}).then(function(v){
    return v;
});

var pTwo = new Promise(function(callback){
    setTimeout(callback, 100, false);
}).then(function(v){
    return v;
});

Promise.all([pOne, pTwo]).then(function(values){
    console.log(values);
});

pOne and pTwo have to resolve with a value in order for that value to be passed to the result of Promise.all . pOnepTwo必须使用一个值进行解析 ,以便将该值传递给Promise.all的结果。

var pOne = new Promise(function(callback){
    setTimeout(function(){
        callback(false);
    }, 100);
}).then(function(v){
    console.log("pOne: " + v);
    return v;
});

Notice the return v inside of the .then callback. 注意.then回调内部的return v That means that the pOne promise is going to resolve with that value ( v in this case being whatever the previous Promise resolved with, or in this case, false . 这意味着pOne承诺将使用该值进行解析 (在这种情况下, v是先前的Promise解析为的值,或者在这种情况下为false

Now do the same for the pTwo promise. 现在对pTwo许诺做同样的pTwo

var pTwo = new Promise(function(callback){
    setTimeout(function(){
        callback(true);
    }, 100);
}).then(function(v){
    console.log("pTwo: " + v);
    return v;
});

Again, we have to return a value from the .then callback function in order for the Promise to resolve with a value, rather than with undefined. 同样,我们必须从.then回调函数返回一个值,以便Promise用一个值而不是未定义的值进行解析。

Now, Promise.all is going to run the Promises, and when (or if) they resolve (in our case they always do), it's going to get the resolved value from each one, and the Promise.all promise itself is going to resolve with the values. 现在, Promise.all将运行Promises,并且当(或如果)他们解决(在我们的情况下,他们总是这样做),它将从每个解决方案中获取已解决的价值,而Promise.all承诺本身将用值解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM