Javascript-未捕获即使已被拒绝的承诺也被拒绝了 [英] Javascript -Uncaught A promise was rejected even though it had already been rejected

查看:79
本文介绍了Javascript-未捕获即使已被拒绝的承诺也被拒绝了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行此Parse.com云代码作业.它查询我的一个类并获取URL,然后读取这些URL(它们是xml文件),然后从它们中获取一些数据并将其保存以进行解析.从代码中可以看出.

I have this Parse.com cloud code job, that I am running. It queries one of my classes and gets url's, then I read these urls, which are xml files, and I get some data from them and save it to parse. As can be seen in the code.

这是代码

此处的完整代码 gist.github.com/gouegd/aae61aa08b8295d52b08

当我运行此云代码作业时.在控制台中.我看到此消息

When I run this cloud code job. In the console. I see this message

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

这是因为一些URL无效,然后代码中断了.

This is coming up because some of the urls are not valid, and then the code breaks.

基本上,我需要一种在其中一个URL不起作用且没有代码停止的情况下处理它的方法!并继续使用其他网址.

Basically I need a way to handle it when one of the URL isn't working and not have the code stop!! And continue with the other urls.

此问题发生在第77-83行之间,其中传递了url变量,因此我需要它忽略错误的url,然后继续处理其余的url.

This problem occurs between line 77-83, where the url variable is being passed, so I need it to ignore bad urls and then keep going with the rest of the urls.

非常感谢您的帮助.

推荐答案

这是一个奇怪的错误消息.

That's a weird error message.

据我所知...

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

Between lines 89 and 90 insert :

    }, 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的承诺不是可投掷安全的",但这种方法很杂乱但可以忍受:

As both of those have failed to handle the error, you might try this, which is messy but tolerable if, as I suspect, Parse's promises are not "throw-safe" :

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.
});

这篇关于Javascript-未捕获即使已被拒绝的承诺也被拒绝了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆