角承诺使用多个http请求

时间:2023-01-06 19:39:06

I have read a few forum posts before on angular promises but can't get it to work in my instance. I am using nodejs /locomotive for the backend and Angular form the frontend.

我之前在论坛上读过几篇关于有棱角的承诺的文章,但在我的例子中,我无法让它发挥作用。我用nodejs /locomotive作为后端和前端的角。

I have the following code in a controller, basically I want to use the path to slides.path, how would I go about doing this using promises? any help would be gratefully received.

我在控制器中有下面的代码,基本上我想用幻灯片的路径。path,我该怎么用承诺来做呢?我们将感激地接受任何帮助。

function ProductCtrl($scope, $http, $q) {

    $scope.events = [];
    $scope.times = [];

    var html = [];
    var chapters = [];
    var path;

    //var paPromise = $q.defer();

    $http({
        url: '/show',
        method: 'GET',
        params: { eventid:$scope.$routeParams.eventid}

    }).success(function(response, code) {

        $scope.events = response;

        angular.forEach($scope.events.slides, function(slide) {

            $http({
                url: '/upload',
                method: 'GET',
                params: {uploadid: slide.upload.toString()}

            }).success(function(response, code) {
                return "http://www.example.com/"+response.path;

            },path);

            slide.path = path;

            chapters.push(slide);

        });

    });
}

1 个解决方案

#1


4  

You can use $q.all do get this multiple promise problem done. Like this:

您可以使用$ q。所有人都完成了这个多重承诺的问题。是这样的:

function ProductCtrl($scope, $http, $q) {

$scope.events = [];
$scope.times = [];

var html = [];

var path;

function fetchChapter() {
    var chapters = [];

    var httpPromise = $http({
        url: '/show',
        method: 'GET',
        params: {
            eventid: $scope.$routeParams.eventid
        }
    });

    //Return the value http Promise
    return httpPromise.then(function (response) {
        $scope.events = response;
        var promises = [];
        angular.forEach($scope.events.slides, function (slide) {

            var inPromise = $http({
                url: '/upload',
                method: 'GET',
                params: {
                    uploadid: slide.upload.toString()
                }
            }).then(function (response, code) {
                //each promise makes sure, that he pushes the data into the chapters
                slide.path = "http://www.example.com/" + response.path;
                chapters.push(slide);
            });
            //Push the promise into an array
            promises.push(inPromise);
        });
        //return the promise from the $q.all, that makes sure, that all pushed promises are ready and return the chapters.
        return $q.all(promises).then(function () {
            return chapters;
        });
    });
}

fetchChapter().then(function(chapters){
   //populate here 
});

}

}

The httpPromise will return the promise from $q.all.

httpPromise将从$q.all返回承诺。

EDIT: How to fetch the data then Wrap a function around, i did with fetchChapter and pass a function into the then there will be the value you need as a parameter.

编辑:如何获取数据然后包装一个函数,我对fetchChapter进行了操作,然后将一个函数传递到。

#1


4  

You can use $q.all do get this multiple promise problem done. Like this:

您可以使用$ q。所有人都完成了这个多重承诺的问题。是这样的:

function ProductCtrl($scope, $http, $q) {

$scope.events = [];
$scope.times = [];

var html = [];

var path;

function fetchChapter() {
    var chapters = [];

    var httpPromise = $http({
        url: '/show',
        method: 'GET',
        params: {
            eventid: $scope.$routeParams.eventid
        }
    });

    //Return the value http Promise
    return httpPromise.then(function (response) {
        $scope.events = response;
        var promises = [];
        angular.forEach($scope.events.slides, function (slide) {

            var inPromise = $http({
                url: '/upload',
                method: 'GET',
                params: {
                    uploadid: slide.upload.toString()
                }
            }).then(function (response, code) {
                //each promise makes sure, that he pushes the data into the chapters
                slide.path = "http://www.example.com/" + response.path;
                chapters.push(slide);
            });
            //Push the promise into an array
            promises.push(inPromise);
        });
        //return the promise from the $q.all, that makes sure, that all pushed promises are ready and return the chapters.
        return $q.all(promises).then(function () {
            return chapters;
        });
    });
}

fetchChapter().then(function(chapters){
   //populate here 
});

}

}

The httpPromise will return the promise from $q.all.

httpPromise将从$q.all返回承诺。

EDIT: How to fetch the data then Wrap a function around, i did with fetchChapter and pass a function into the then there will be the value you need as a parameter.

编辑:如何获取数据然后包装一个函数,我对fetchChapter进行了操作,然后将一个函数传递到。