我有一个二维数组(2 X n)的承诺,我想首先处理第1行中的所有承诺,然后处理第2行中的所有承诺。
换句话说,我希望在另一组承诺完成执行后链接一组承诺。
我怎样才能做到这一点?
我尝试了以下但是它不起作用。有什么想法吗?
Promise.resolve()
.then(createPromises)//generates topPromises= [ [p1, p2, p3], [p4,p5,p6]]
.then(function(topPromises){
Promise.all(topPromises[0])
.then(Promise.all[topPromises[1])
})
答案 0 :(得分:2)
您错过了return
,并在Promise
function
而不是.then
Promise.resolve()
.then(createPromises)//generates topPromises= [ [p1, p2, p3], [p4,p5,p6]]
.then(function(topPromises){
return Promise.all(topPromises[0])
.then(() => Promise.all[topPromises[1]))
})
我不确切地知道您需要什么,但可能您的代码可能是这样的:
cratePromises().reduce((topPromise, nestedPromises) => {
return topPromise.then(data => {
// data is empty array on first iteration and array of nestedPromises values on next iterations
return Promise.all(nestedPromises)
})
}, Promise.resolve([]))
答案 1 :(得分:1)
你可以使用一点伪递归来解决这个问题。
基本上,你有一个完成一组承诺的功能。完成后,再次调用相同的函数,但使用下一组。
/* Declare */
CountryPickerDialog countryPicker;
/* Name of your Custom JSON list */
int resourceId = getResources().getIdentifier("country_avail", "raw", getApplicationContext().getPackageName());
countryPicker = new CountryPickerDialog(MainActivity.this, new CountryPickerCallbacks() {
@Override
public void onCountrySelected(Country country, int flagResId) {
/* Get Country Name: country.getCountryName(context); */
/* Call countryPicker.dismiss(); to prevent memory leaks */
}
/* Set to false if you want to disable Dial Code in the results and true if you want to show it
Set to zero if you don't have a custom JSON list of countries in your raw file otherwise use
resourceId for your customly available countries */
}, false, 0);
countryPicker.show();