2016-07-29 82 views
4

我面临着使用$q#all方法返回承诺的问题。按顺序退回承诺

我要做出承诺,互相依赖,

如果我设置obj1, obj2 and obj3我想,让他们在同一顺序

我该如何做到这一点?

Factory:

mainFactory.$inject = ['$http', '$q']; 

function mainFactory($http, $q) { 
    var mainFactory = { 
    getPromises: getPromises 
    }; 

    return mainFactory; 

    function getPromises(id) { 
    promises = { 
     'obj1': $http.get('http1'), 
     'obj2': $http.get('http2'), 
     'obj3': $http.get('http3'), 
     'obj4': $http.get('http4', { params: { 'id': id } }), 
     'obj5': $http.get('http5'), 
     'obj6': $http.get('http6', { params: { 'id': id } }) 
    }; 

    return $q.all(promises); 
    } 
} 

Controller:

MainCtrl.$inject = ['mainFactory']; 

function MainCtrl(mainFactory) { 
    var vm = this; 
    mainFactory.getPromises(id) 
    .then(getResponse) 
    .catch(getError); 

    function getResponse(response) { 
    var keys = Object.keys(response), i = keys.length; 
    while (i--) { 
     var key = keys[i]; 
     console.log(key); // I want all the keys in order, i.e. => obj1, obj2.. and so on 
     var value = response[key].data; 
     switch(key) { 
     ... 
     } 
    } 
    } 

    function getError(error) { 
    console.log(error); 
    } 
} 

编辑:

我也试过这样:

var promises = {}; 
return $http.get('/admin/http1.json').then(function (value) { 
    promises['obj1'] = value; 
    }) 
.then(function (result) { 
    return $http.get('/admin/http2.json').then(function (value) { 
    promises['obj2'] = value; 
    }); 
}).then(function (result) { 
    return $http.get('/admin/http3.json').then(function (value) { 
    promises['obj3'] = value; 
    }); 
});  
return $q.all(promises); 
+1

我不擅长承诺,对不起,但另一种方法是使用http://caolan.github.io/async/docs.html#.eachOfSeries – Plato

+6

对象键天生*无序*。改用数组。 –

+0

看看$ q.serial()上的这篇文章。我认为这将是你正在寻找的。 http://www.codeducky.org/q-serial/ – mhodges

回答

1

编辑2

错误,我只是复制你的代码上面没有意识到这是一个对象。大声笑。

promises = [ 
    $http.get('http1'), 
    $http.get('http2'), 
    $http.get('http3'), 
    $http.get('http4', { params: { 'id': id } }), 
    $http.get('http5'), 
    $http.get('http6', { params: { 'id': id } }) 
] 

编辑1

对不起,我没有注意到的评论Jared Smith是写。

Object keys are inherently unordered. Use an array instead.

编辑0

对象键不会被订购。使用数组声明您的承诺。

promises = [ 
    $http.get('http1'), 
    $http.get('http2'), 
    $http.get('http3'), 
    $http.get('http4', { params: { 'id': id } }), 
    $http.get('http5'), 
    $http.get('http6', { params: { 'id': id } }) 
] 

$q.all(promises) 
    .then(functions(resolves){ 
     // resolves here is an array 
    }).catch(function(err){ 
     // throw err 
    }); 
+0

'未捕获的SyntaxError:意外的标记:'。不知道这是否应该是一个对象...但它绝对是无效的JavaScript。 –

+0

嗯,诚实的错误@PatrickRoberts请阅读**编辑2 ** – CENT1PEDE

1

使用$q.all将解决在没有特定的顺序每个承诺。如果您希望在每个承诺解决后执行它们,请使用承诺链接

function getPromises(id) { 
    var getObjA = function() { 
    return $http.get('http1'); 
    }; 

    var getObjB = function() { 
    return $http.get('http2'); 
    }; 

    var getObjC = function() { 
    return $http.get('http3'); 
    }; 

    var getObjD = function() { 
    return $http.get('http4', { params: { 'id': id } }); 
    }; 

    var getObjE = function() { 
    return $http.get('http5'); 
    }; 

    var getObjF = function() { 
    return $http.get('http5', { params: { 'id': id } }); 
    }; 

    return getObjA() 
    .then(getObjB) 
    .then(getObjC) 
    .then(getObjD) 
    .then(getObjE) 
    .then(getObjF); 
} 

编辑:作为一个额外的信息,您可以通过放置一个catch语句这里

getPromises("id") 
    .then(<success callback here>) 
    .catch(<error callback that will catch error on any of the promises>); 

意义赶在该等承诺的任何错误,一旦承诺失败,所有下方止跌随后承诺将不会被执行,并会被你的捕获声明捕获