1

어떻게 이러한 서로 다른 호출의 결과를 비교할 수 있습니까? URL과 과일 목록은 다른 json 파일에 저장되어 있으며 약속하기 전에이를 비교할 것입니다. 그들은 서로 기다려야하고 공통의 약속으로 성취해야합니다.

angular.module('myApp', ['ngResource']) 
    .controller('MainCtrl', function($scope, $http, $resource) { 
    var list = {}; 
    $http.get('images.json').then(function(response) { 
     response.data.objects.forEach(function(images) { 
     list[images.id] = images.url; 
     }); 
    }); 
    $resource('fruits.json').get().$promise.then(function(response) { 
     var list = { 
     1: 'badUrlExampleForApple', 
     2: 'badUrlExampleForOrange', 
     3: 'badUrlExampleForPineapple' 
     } 
     response.objects.forEach(function(product) { 
     if (list[product.id]) { 
      console.log(product.name + ': ' + list[product.id]); 
     } 
     }); 
    }); 
    }); 

나는 조금 비트 feird 알고,하지만 난이 방법으로 그것을 할 수 있습니다.

Plunker 예제 : http://plnkr.co/edit/ICwvYI?p=preview

어떤 도움을 크게 감상 할 수있다!

답변

2

당신은 누구의 .then 펼쳤다 약속을 얻을 $q.all를 사용 둘 다 :

var images = $http.get('images.json'); 
var fruits = $resource('fruits.json').get().$promise; 

$q.all([images,fruits]).then(function(results){ 
    var images = results[0]; 
    var fruits = results[1]; 
    // access to both here, you can compare anything you want 
}); 

$q.all에 대한 가이드는 말한다 :

이의 경우를 모두 해결하는 하나의 약속에 여러 약속을 결합 입력 약속이 해결됩니다. 블루 버드와 같은 다른 약속 라이브러리에서와 ES6의 약속을

, thatr 기능은 아마 Q.all의 원래의 질문에, Promise.all이 될 것입니다.

+0

고맙습니다! 잘 작동합니다! :) – kree