Javascript-承诺保持挂起状态

Javascript-承诺保持挂起状态,javascript,promise,es6-promise,Javascript,Promise,Es6 Promise,为什么我的承诺停留在悬而未决的状态,我该如何解决它 var foundPeopleA = findPeopleA().then(function(result) { var res = [] result.map(function(el) { res.push(getProfileXML(el.sid)); }); return res }); var foundPeopleB =

为什么我的承诺停留在悬而未决的状态,我该如何解决它

    var foundPeopleA = findPeopleA().then(function(result) {
        var res = []
        result.map(function(el) {
            res.push(getProfileXML(el.sid));
        });
        return res
    });

    var foundPeopleB = findPeopleB().then(function(result) {
        var res = []
        result.map(function(el) {
            res.push(getProfileXML(el.sid));
        });
        return res
    })

    return Promise.all([findPeopleA, findPeopleB]).then(function(results) {
        console.log(results) //[ [ Promise { <pending> }, Promise { <pending> } ],  [ Promise { <pending> }, Promise { <pending> } ] ]
    })

它们不会挂起,我会得到结果。

数组不是承诺。如果您返回一个承诺数组,
将获得一个承诺数组,就像您返回任何其他非承诺值一样。只有当您返回承诺时,承诺才会在
之前执行,然后
。您的
foundpeopreak
foundPeopleB
都构建了一系列承诺;您需要连接这些数组并将它们传递给Promise。所有的
或等效的数组才能执行它们。

数组不是Promise。如果您返回一个承诺数组,
将获得一个承诺数组,就像您返回任何其他非承诺值一样。只有当您返回承诺时,承诺才会在
之前执行,然后
。您的
foundpeopreak
foundPeopleB
都构建了一系列承诺;您需要连接这些数组并将它们传递给Promise.all
或等效程序,以便执行它们。

尝试将数组分配给映射结果

var foundPeopleA = findPeopleA().then(function(result) {
    var res = []
    res = result.map(function(el) {
        return getProfileXML(el.sid);
    });
    return res
});
或者,也许你可以解决这个承诺

var foundPeopleA = findPeopleA().then(function(result) {
    var res = []
    res = result.map(function(el) {
        return getProfileXML(el.sid);
    });
    resolve(res);
});

无论哪种方法,我相信您都需要通过从映射返回值来构建数组,以创建新数组。

尝试将数组分配给映射结果

var foundPeopleA = findPeopleA().then(function(result) {
    var res = []
    res = result.map(function(el) {
        return getProfileXML(el.sid);
    });
    return res
});
或者,也许你可以解决这个承诺

var foundPeopleA = findPeopleA().then(function(result) {
    var res = []
    res = result.map(function(el) {
        return getProfileXML(el.sid);
    });
    resolve(res);
});

无论采用哪种方式,我相信您都需要通过从映射返回值来创建新数组来构建数组。

问题在于,您正在使用
然后
分别处理每个数组上的承诺履行,而
all
通过传递一系列未解决的承诺来处理多个承诺的履行。它建立了一个新的承诺,将所有这些成果结合在一起。只需使用:

Promise.all([findPeopleA(), findPeopleB()])
.then(function(responses) ...

问题在于,您正在使用
然后
分别处理每个承诺的履行,而
all
通过传递一系列未解决的承诺来处理多个承诺的履行。它建立了一个新的承诺,将所有这些成果结合在一起。只需使用:

Promise.all([findPeopleA(), findPeopleB()])
.then(function(responses) ...

第一个给出了相同的输出,第二个甚至没有到达下一次执行,无法得到输出。你说的“相同的输出”是什么意思?我在问题中提到的那个//[[Promise{},Promise{}],[Promise{},Promise{}]]第一个给出相同的输出,第二个甚至没有到达下一个执行,无法得到输出。你说的“相同的输出”是什么意思?我在问题中提到过的//[Promise{},Promise{}],[Promise{},Promise{}]