gpt4 book ai didi

javascript - angularjs 返回被拒绝的 promise

转载 作者:行者123 更新时间:2023-11-29 23:46:01 25 4
gpt4 key购买 nike

我这里有一些奇怪的行为......我创建了一项服务来处理所有 API 调用。它看起来像这样:

angular.module('widget.data').service('apiHandler', apiHandler);

apiHandler.$inject = ['$http', 'SimpleCache', 'apiUrl', 'ngNotify'];

function apiHandler($http, simpleCache, apiUrl, ngNotify) {

return {
url: apiUrl,

get: get,
post: post,
put: put,
delete: requestDelete
};

//////////////////////////////////////////////////

// GET
function get(url, params) {
return buildRequest(url, 'GET', null, params);
};

// POST
function post(url, data) {
return buildRequest(url, 'POST', data);
};

// PUT
function put(url, data) {
return buildRequest(url, 'PUT', data);
};

// DELETE
function requestDelete(url, params) {
return buildRequest(url, 'DELETE', null, params);
};

// Private function to build our request
function buildRequest(url, method, data, params) {

//console.log(url);

// Create our apiPath
var apiPath = url.indexOf('http') > -1 ? url : apiUrl + url;

// Create the model
var model = {
method: method,
url: apiPath,
data: data,
params: params,
cache: simpleCache
};

// If we are performing an update/create or delete call
if (method !== 'GET') {

// Remove from our cache
simpleCache.remove(apiUrl + url);
}

// Build our request
return $http(model).then(function (response) {

// Return our response
return response.data;

// If we have an error
}, function (response) {

console.log(response.data.exceptionMessage);

// Display our error
ngNotify.set(response.data.exceptionMessage, { type: 'error' });

// Return our error
return response;
});
};
};

你可以在buildMessage中看到它返回了调用、响应和错误响应。所以我希望如果有错误,任何注入(inject)了这个的服务也会触发错误回调。但事实并非如此。我有这项服务:

angular.module('widget.directives').service('pkAuthenticateService', pkAuthenticateService);

pkAuthenticateService.$inject = ['apiHandler'];

function pkAuthenticateService(apiHandler) {
return {
register: register
};

//////////////////////////////////////////////////

function register(model) {

return apiHandler.post('users/create', model).then(function (response) {
console.log('success', response);
return response;
}, function (response) {
console.log('error', response);
return response;
});
};
};

但是我在控制台中得到的消息是成功消息而不是错误消息。有人可以向我解释为什么吗?或者帮助我让它按我预期的那样工作(即,如果它在父服务中失败,那么它应该一直失败)。

最佳答案

这是 JS promises 中最有趣和最令人困惑的情况之一——“吞噬”错误。考虑以下情况:

var x = new Promise((result,reject)=>{
reject('something bad')
})

x.then(()=>{
console.log('wow')
return 'a';
},()=>{
console.log('ops')
return 'a';
}).then(()=>{
console.log('I\'m baaack')
return 'a';
},()=>{
console.log('still bad?')
return 'a';
})

这可能违反直觉,但在第 2 个 then() 中将打印 'I\'m baaack',因为您已经捕获并处理了错误。因此,如果您正在处理错误,无论是使用 then() 的第二个参数还是使用一些“catch”,您都需要抛出错误或返回一些被拒绝的内容以进一步传递错误。

在您的情况下,无需 $q 即可完成,只需替换 1 行:

        // If we have an error
}, function (response) {

console.log(response.data.exceptionMessage);

// Display our error
ngNotify.set(response.data.exceptionMessage, { type: 'error' });

// Return our error
throw response;
});

关于javascript - angularjs 返回被拒绝的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44118089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com