Ajax Bluebird Promise包装器出现未处理的拒绝错误 [英] Unhandled rejection error with Ajax Bluebird promise wrapper

查看:100
本文介绍了Ajax Bluebird Promise包装器出现未处理的拒绝错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Ajax包装到Bluebird Promise包装器中,但是却收到:

I am trying to wrap Ajax into a Bluebird promise wrapper, but am receiving:

错误:未处理的拒绝(此处有堆栈跟踪...)

Error: Unhandled rejection (stack trace here...)

wrapper1.js

wrapper1.js

let fetch = require('./wrapper2');

function requestWeb(type, url, data) {
    return new Promise(function(resolve, reject) {
        url = config.serverUrl + url.trim();

        let options = {
            type: type,
            data: data ? JSON.stringify(data) : null,
            dataType: 'json',
            contentType: 'application/json',
            crossDomain: true,
            timeout: 15000,
            xhrFields: { withCredentials: true }
        };

        fetch(url, options)
            .then(data => {
                resolve(data);
            })
            .catch(err => {
                console.log('web api error: ' + err.message);
                notify('Please check your interet connection');
                reject(err);
            });
    });
}

wrapper2.js

wrapper2.js

import Promise from 'bluebird';

export default function(url, options) {
    return new Promise(function(resolve, reject) {
        $.ajax(url, options)
            .done((result) => {
                resolve(result);
            })
            .fail((xhr, err) => {
                let proxy = new Error();
                proxy.message = err || 'error is null';
                proxy.name = 'ajax error';

                reject(proxy);
            });
    });
}

请注意蓝鸟在reject()上需要其他错误对象.

推荐答案

我知道了,BlueBird希望警告您已经触发了reject()调用,但您没有抓住它.所以我在用...

I figured it out, BlueBird wants to warn you that a reject() call has been fired but you are not catching it. So I was using...

requestWeb(type, url, data).then((result)=>{});

要解决此问题,请执行以下两项操作之一:将.catch()添加到调用的末尾,或从promise中删除reject(err).

So to fix, do one of two things: add the .catch() to the end of the call, or remove the reject(err) from the promise.

这篇关于Ajax Bluebird Promise包装器出现未处理的拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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