Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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 AngularJS服务返回对象而不是承诺_Javascript_Angularjs_Angular Promise_Q - Fatal编程技术网

Javascript AngularJS服务返回对象而不是承诺

Javascript AngularJS服务返回对象而不是承诺,javascript,angularjs,angular-promise,q,Javascript,Angularjs,Angular Promise,Q,我试图重构AngularJS代码,方法是让我的服务解析一个promise调用,该调用将自动将一个对象绑定到我的作用域 // converting this: imageService.getImages(userId) .then(images => { $scope.images = images; }); // into this: $scope.images = imageService.getImages(userId); 我目前尝试重构的解决方案是: function

我试图重构AngularJS代码,方法是让我的服务解析一个promise调用,该调用将自动将一个对象绑定到我的作用域

// converting this:
imageService.getImages(userId)
.then(images => {
    $scope.images = images;
});

// into this:
$scope.images = imageService.getImages(userId);
我目前尝试重构的解决方案是:

function getImages(userId) {
    const config = {
        method: 'get',
        url: '/a/images',
        params: { userId: userId }
    };

    return $http(config).then(onGetImagesSuccess, onGetImagesError);
}

function onGetImagesSuccess(response) {
    $log.info('Get image success', response);
    return response.data;
}

function onGetImagesError(response) {
    $log.info('Get image error', response);
}
我开始玩弄
$q.defer()
,我的
getImages()
达到以下状态。问题是I
imageService.getImages()
返回一个承诺而不是图像。有趣的是,图像列表确实被记录下来了

function getImages(userId) {
    const deferred = $q.defer();
    const config = {
        method: 'get',
        url: '/a/images',
        params: { userId: userId }
    };

    let call = $http(config).then(onGetImagesSuccess, onGetImagesError);
    deferred.resolve(call);

    return deferred.promise.then(images => {
        $log.info(images); // List of images does get logged
        return images; // Promise gets returned instead of the actual images
    });
}

我想知道我需要做什么来重构我的代码以获得我的
imageService.getImages()
以返回图像列表而不是承诺。

这可以通过
$resource
中使用的相同方法来完成。返回的空对象(数组)异步填充数据。如果需要知道请求何时以及如何完成,还应返回承诺:

var result = {
  data: [],
  promise: $http(...).then(response => {
    return Object.assign(result.data, response.data);
  })
};

return result;
然后在视图中绑定
imageService.data
数组


这种模式有其优点和缺点。在控制器中有
然后
打开承诺是很自然的。

承诺会被返回,而不是实际图像
-是的,而且总是会,因为承诺链总是导致承诺。。。在发出异步请求时,不能使异步结果同步返回(这是您尝试执行的操作)。你没有办法按照你的要求去做。为什么要更改代码?我只是想通过将更多的逻辑从控制器移到服务中来重构代码。我希望,一旦承诺得到解决,服务会将结果返回给我的控制器,而无需我调用
then()
“而无需我调用then”听起来像是要转到async/wait then:)。。。或者返回到callbacks HAold,但具有说明性(注意,您需要try/catch…)