Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在映射请求完成时解析承诺_Javascript_Typescript_Observable - Fatal编程技术网

Javascript 在映射请求完成时解析承诺

Javascript 在映射请求完成时解析承诺,javascript,typescript,observable,Javascript,Typescript,Observable,我正在处理一个书签页面,我们得到带有书签的餐厅id的书签结果。然后我映射响应并将其作为对象推送到数组中 我想解析整个完成的数组,以便以后可以操作数据 我创建了一个getData函数,它向书签api发出请求,在onSucces中,我调用了一个名为mapResult的函数,如下所示: mapResults(result: Array<any>, type: string): Promise<any> { const promise = new Promise<a

我正在处理一个书签页面,我们得到带有书签的餐厅id的书签结果。然后我映射响应并将其作为对象推送到数组中

我想解析整个完成的数组,以便以后可以操作数据

我创建了一个getData函数,它向书签api发出请求,在onSucces中,我调用了一个名为mapResult的函数,如下所示:

mapResults(result: Array<any>, type: string): Promise<any> {
    const promise = new Promise<any>((resolve, reject) => {
        const requests = result.map((res, i) => {
            this.getRestaurantById(res.RestaurantId).then(restaurant => {
                const bookmarks = {
                     createdDate: res.CreatedDate,
                     restaurant: restaurant[0]
                };
                this.savedData[type].push(bookmarks);
            });
        });
        Promise.all(requests).then((completed) => {
            if(completed) {
                console.log(completed)
                resolve(this.savedData[type]);
            }
        })
    });
    return promise;
}
但是
data
console.log不是整个数据数组,它只解析第一个对象


为什么Promis.all功能不等待映射完成?

您没有在映射中返回您的承诺。

您的代码中有几个问题:

  • result.map
    回调中,您不会
    返回任何内容
  • 不需要新承诺
  • 您不应该使用状态变量而不是承诺返回值
  • completed
    变量没有任何意义
此代码应按预期工作:

mapResults(result: Array<any>, type: string): Promise<any> {
    // you don't need "new Promise" as "getRestaurantById" already returns a promise itself

    const requests = result.map((res) => {
        // here you forgot to return something
        return this.getRestaurantById(res.RestaurantId).then(restaurant => {
            return {
                createdDate: res.CreatedDate,
                restaurant: restaurant[0]
            };
        });
    });

    // using "completed" did not make any sense as it is just an array filled with "undefined"s
    return Promise.all(requests).then((restaurants) => {
        console.log(restaurants)
        // TODO store "restaurants" in "this.savedData[type]" if needed
        return restaurants;
    });
}
mapResults(结果:数组,类型:字符串):Promise{
//您不需要“新承诺”,因为“getRestaurantById”本身已经返回了一个承诺
const requests=result.map((res)=>{
//你忘了还东西了
返回这个.getRestaurantById(res.RestaurantId)。然后(restaurant=>{
返回{
createdDate:res.createdDate,
餐厅:餐厅[0]
};
});
});
//使用“completed”没有任何意义,因为它只是一个填充了“undefined”的数组
返回承诺。所有(请求)。然后((餐厅)=>{
控制台日志(餐厅)
//如果需要,TODO将“餐厅”存储在“this.savedData[type]”中
返回餐厅;
});
}

谢谢你的回答,但即使我做了同样的结果。谢谢你详细的回答,这确实解决了问题!:)
mapResults(result: Array<any>, type: string): Promise<any> {
    // you don't need "new Promise" as "getRestaurantById" already returns a promise itself

    const requests = result.map((res) => {
        // here you forgot to return something
        return this.getRestaurantById(res.RestaurantId).then(restaurant => {
            return {
                createdDate: res.CreatedDate,
                restaurant: restaurant[0]
            };
        });
    });

    // using "completed" did not make any sense as it is just an array filled with "undefined"s
    return Promise.all(requests).then((restaurants) => {
        console.log(restaurants)
        // TODO store "restaurants" in "this.savedData[type]" if needed
        return restaurants;
    });
}