Javascript 使用多个http请求

Javascript 使用多个http请求,javascript,jquery,node.js,angularjs,promise,Javascript,Jquery,Node.js,Angularjs,Promise,我以前读过一些论坛上关于角度承诺的帖子,但在我的例子中无法实现。我使用nodejs/motor作为后端,而使用Angular作为前端 我在控制器中有以下代码,基本上我想使用幻灯片的路径。路径,我如何使用承诺来实现这一点?任何帮助都将受到感激 function ProductCtrl($scope, $http, $q) { $scope.events = []; $scope.times = []; var html = []; var chapters =

我以前读过一些论坛上关于角度承诺的帖子,但在我的例子中无法实现。我使用nodejs/motor作为后端,而使用Angular作为前端

我在控制器中有以下代码,基本上我想使用幻灯片的路径。路径,我如何使用承诺来实现这一点?任何帮助都将受到感激

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

        });

    });
}

您可以使用$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 
});
}

httpPromise将从$q.all返回承诺

编辑:如何获取数据
我使用了
fetchChapter
来包装一个函数,并将一个函数传递到
中,然后
将有您需要的值作为参数。

您可以使用$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 
});
}

httpPromise将从$q.all返回承诺

编辑:如何获取数据
我用
fetchChapter
完成了一个函数的包装,并将一个函数传递到
中,然后
将有您需要的值作为参数。

您这样做是有承诺的$http返回一个承诺,并且只有在承诺被解析时才会调用成功调用。您到底有什么问题?除非您想完全使用FRP或启用传统自动退绕模式。我不确定你想完成什么,你是在做承诺$http返回一个承诺,并且只有在承诺被解析时才会调用成功调用。您到底有什么问题?除非您想完全使用FRP或启用传统自动退绕模式。我不知道你想完成什么。为什么不从承诺中返回
章节
?你可以简单地将切片映射到它们上面的承诺。“s”缺少^^
章节。推(滑)
谢谢你的回答,我如何使用var章节填充httpromise之外的div呢?我已经开始理解承诺了,酷,谢谢。为什么不从承诺中返回
章节
?您只需将切片映射到它们上的承诺即可。“s”缺少^^
章节。推(滑)
感谢您的回复我如何使用var章节填充HttpPromise之外的div我已经开始理解承诺,酷,干杯