Promise.all()返回未定义

时间:2018-11-02 20:09:39

标签: javascript arrays promise es6-promise

我有三个宠物名字的数组。一个是不正确的。目标是从api获取宠物,如果不存在宠物,则返回仅具有现有宠物名称的更改数组。问题是Promise.all()返回未定义。

kubectl describe job <job-name> -n <namespace>

1 个答案:

答案 0 :(得分:2)

尝试一下:

const getValidPets = async (pets) => {
    const petsArray = pets.map(pet => getPetHttp(pet).then(record => record.name).catch(() => undefined));

    return Promise.all(petsArray).then((filteredPets) => {
        const arr = filteredPets.filter(petName => petName !== undefined);
        return arr; // ["Rex", "Jug"] - here is correct!
    });
};

const pets = ["Rex", "Jug", "some_wrong_name"];
const finalPets = await getValidPets(pets); // undefined - incorrect