Javascript-Uncaught一个承诺被拒绝了,尽管它已经被拒绝了

Javascript -Uncaught A promise was rejected even though it had already been rejected

本文关键字:拒绝 一个承诺 Javascript-Uncaught      更新时间:2023-09-26

我有一个正在运行的Parse.com云代码作业。它查询我的一个类并获取url,然后我读取这些url,它们是xml文件,我从中获取一些数据并保存以进行解析。如代码中所示。

这是代码

完整代码在这里gist.github.com/gouegd/aae61aa08b8295d52b08

当我运行这个云代码作业时。在控制台中。我看到这个消息

Failed with: Uncaught A promise was rejected even though it had already been rejected.

出现这种情况是因为一些url无效,然后代码中断。

基本上,我需要一种方法来处理它,当其中一个URL不工作,并且没有停止代码!!并继续使用其他URL。

这个问题发生在第77-83行之间,在那里传递url变量,所以我需要它忽略坏的url,然后继续使用其余的url。

感谢您提前提供的帮助。

这是一条奇怪的错误消息。

据我所知。。。

在第89行和第90行之间插入:

    }, function() {
        return Parse.Promise.as();//return resolved promise to keep the promise chain going.

给予:

return Parse.Cloud.httpRequest({
    url: url,
    //data: ... //some properties of menuItem?
}).then(function(httpResponse) {
    return readResponse_async(httpResponse.text).then(function(res) {
        if (res.menu.day.at(dayNumber).meal) {
            return saveMeals_async(res.menu.day.at(dayNumber).meal, school, diningHallNumber, menuLocation);
        } else {
            return Parse.Promise.as();//return resolved promise to keep the promise chain going.
        }
    }, function() {
        return Parse.Promise.as();//return resolved promise to keep the promise chain going.
    });
});

或者可能低一行:

return Parse.Cloud.httpRequest({
    url: url,
    //data: ... //some properties of menuItem?
}).then(function(httpResponse) {
    return readResponse_async(httpResponse.text).then(function(res) {
        if (res.menu.day.at(dayNumber).meal) {
            return saveMeals_async(res.menu.day.at(dayNumber).meal, school, diningHallNumber, menuLocation);
        } else {
            return Parse.Promise.as();//return resolved promise to keep the promise chain going.
        }
    });
}, function() {
    return Parse.Promise.as();//return resolved promise to keep the promise chain going.
});

编辑

由于这两个都未能处理错误,您可以尝试这样做,这是混乱的,但如果像我怀疑的那样,Parse的承诺不是"安全的":

return Parse.Cloud.httpRequest({
    url: url,
    //data: ... //some properties of menuItem?
}).then(function(httpResponse) {
    try {
        return readResponse_async(httpResponse.text).then(function(res) {
            if (res.menu.day.at(dayNumber).meal) {
                return saveMeals_async(res.menu.day.at(dayNumber).meal, school, diningHallNumber, menuLocation);
            } else {
                throw new Error();
            }
        });
    }
    catch(e) {
        return Parse.Promise.as();//return resolved promise to keep the promise chain going.
    }
}, function() {
    return Parse.Promise.as();//return resolved promise to keep the promise chain going.
});