我怎样才能使递归 Promise 调用成为 return 以减少 Javascript?
How can I make a recursive Promise call to be the return for a reduce in Javascript?
我正在尝试在 JS 中的 reduce
中创建递归 Promise
调用。我的系统在这里的目标是对 reduce
播放的数组中的每个项目进行 n 次大调用,然后,如果在 reduce
中那个大调用决定它需要 n 次较小的调用才能得到回到大项,那么 reduce
不应该跳到下一项,只是等待它完成。
我当前的代码是:
function download_preliminary_files_1() {
let demo_schema_download = new Base_Ajax_Requester(
localized_data.ajax_url,
{
'ajax_action': 'download_demo_file_schema',
'ajax_nonce': localized_data.ajax_nonce,
'backend': {
'data_handle': 'download_demo_file_schema_data',
'data':
{
'demo_handle' : 'demo-2',
}
}
}
);
let import_files_download = demo_schema_download.call().then(function() {
fake_data.steps_to_import.reduce(function(previous_promise, next_step_identifier) {
return previous_promise.then(function() {
let file_download = download_demo_file({
'demo_handle' : 'demo-2',
'step' : next_step_identifier
}).call();
file_download.then(function(response) {
/**
* Here, if I detect that response.remaining_number_of_files > 0, I should start
* a chain that keeps calling `download_demo_file` with new parameters.
*
* Then, when this chain is done, resume the normal reduce behavior.
*/
});
});
}, Promise.resolve())
}).catch(function(error) {
console.log( 'Got this error:' + error);
});
return import_files_download;
}
其中 Base_Ajax_Requester
是一个助手 class,它处理 AJAX 请求,returns 是 Promise
,当它完成时,可以围绕它编写代码。
我的 fake_data
是:
let fake_data = {
'demo_handle' : 'demo-2',
'steps_to_import' : [ 'elementor-hf','post', 'nav_menu' ]
}
如您所见,fake_data.steps_to_import.reduce(..
将遍历这 3 个值,对每个值调用 download_demo_file
,等待它完成,然后继续下一个。我们可以说我想在 elementor-hf
和 post
之间进行 n 个较小的调用。
上面显示了对 download_demo_file
的初始调用,这是始终从后端返回的内容:
{
'message' : '...',
'file_number' : 1, //Which -n.xml file has been downloaded where n is this number.
'remaining_number_of_files' : 1 //Calculates, based on what file was downloaded how many files are left. The system knows internally how many files it has to download.
}
使用 download_demo_file
的重试调用看起来像:
{
'demo_handle' : 'demo-2',
'step' : next_step_identifier,
'file_counter' : 2 //Dynamic. This will signal that it needs to now download file-2.xml.
}
...以此类推,直到后端发送remaining_number_of_files : 0
,然后就全部停止了,因为没有更多的文件可以下载了,可以跳到下一个big call。
我怎样才能做到这一点?
在每个 reduce
回调中,我将创建一个调用 download_demo_file
的函数(带有变化的 file_counter
),然后 Promise 解析,递归地 returns if remaining_number_of_files > 0
自身的调用。这意味着 getProm()
将不断调用自身,直到 remaining_number_of_files > 0
条件不再满足,并且只有在那之后,特定 reduce
迭代的整个 Promise 才会解析。
let import_files_download = demo_schema_download.call().then(function() {
fake_data.steps_to_import.reduce(function(previous_promise, step) {
let file_counter = 1;
return previous_promise.then(function() {
const getProm = () => download_demo_file({
demo_handle: 'demo-2',
step,
file_counter
}).call()
.then((response) => {
file_counter++;
return response.remaining_number_of_files > 0
? getProm()
: response
});
return getProm();
});
}, Promise.resolve())
}).catch(function(error) {
console.log('Got this error:' + error);
});
使用 async
/await
代码可能 很多 更容易阅读和理解,但是:
await demo_schema_download.call();
const import_files_download = [];
for (const step of fake_data.steps_to_import) {
let response;
let file_counter = 1;
do {
response = await download_demo_file({
demo_handle: 'demo-2',
step,
file_counter
}).call();
file_counter++;
} while (response.remaining_number_of_files > 0);
import_files_download.push(response); // is this needed?
}
return import_files_download; // is this needed?
捕获异步函数的使用者。
我正在尝试在 JS 中的 reduce
中创建递归 Promise
调用。我的系统在这里的目标是对 reduce
播放的数组中的每个项目进行 n 次大调用,然后,如果在 reduce
中那个大调用决定它需要 n 次较小的调用才能得到回到大项,那么 reduce
不应该跳到下一项,只是等待它完成。
我当前的代码是:
function download_preliminary_files_1() {
let demo_schema_download = new Base_Ajax_Requester(
localized_data.ajax_url,
{
'ajax_action': 'download_demo_file_schema',
'ajax_nonce': localized_data.ajax_nonce,
'backend': {
'data_handle': 'download_demo_file_schema_data',
'data':
{
'demo_handle' : 'demo-2',
}
}
}
);
let import_files_download = demo_schema_download.call().then(function() {
fake_data.steps_to_import.reduce(function(previous_promise, next_step_identifier) {
return previous_promise.then(function() {
let file_download = download_demo_file({
'demo_handle' : 'demo-2',
'step' : next_step_identifier
}).call();
file_download.then(function(response) {
/**
* Here, if I detect that response.remaining_number_of_files > 0, I should start
* a chain that keeps calling `download_demo_file` with new parameters.
*
* Then, when this chain is done, resume the normal reduce behavior.
*/
});
});
}, Promise.resolve())
}).catch(function(error) {
console.log( 'Got this error:' + error);
});
return import_files_download;
}
其中 Base_Ajax_Requester
是一个助手 class,它处理 AJAX 请求,returns 是 Promise
,当它完成时,可以围绕它编写代码。
我的 fake_data
是:
let fake_data = {
'demo_handle' : 'demo-2',
'steps_to_import' : [ 'elementor-hf','post', 'nav_menu' ]
}
如您所见,fake_data.steps_to_import.reduce(..
将遍历这 3 个值,对每个值调用 download_demo_file
,等待它完成,然后继续下一个。我们可以说我想在 elementor-hf
和 post
之间进行 n 个较小的调用。
上面显示了对 download_demo_file
的初始调用,这是始终从后端返回的内容:
{
'message' : '...',
'file_number' : 1, //Which -n.xml file has been downloaded where n is this number.
'remaining_number_of_files' : 1 //Calculates, based on what file was downloaded how many files are left. The system knows internally how many files it has to download.
}
使用 download_demo_file
的重试调用看起来像:
{
'demo_handle' : 'demo-2',
'step' : next_step_identifier,
'file_counter' : 2 //Dynamic. This will signal that it needs to now download file-2.xml.
}
...以此类推,直到后端发送remaining_number_of_files : 0
,然后就全部停止了,因为没有更多的文件可以下载了,可以跳到下一个big call。
我怎样才能做到这一点?
在每个 reduce
回调中,我将创建一个调用 download_demo_file
的函数(带有变化的 file_counter
),然后 Promise 解析,递归地 returns if remaining_number_of_files > 0
自身的调用。这意味着 getProm()
将不断调用自身,直到 remaining_number_of_files > 0
条件不再满足,并且只有在那之后,特定 reduce
迭代的整个 Promise 才会解析。
let import_files_download = demo_schema_download.call().then(function() {
fake_data.steps_to_import.reduce(function(previous_promise, step) {
let file_counter = 1;
return previous_promise.then(function() {
const getProm = () => download_demo_file({
demo_handle: 'demo-2',
step,
file_counter
}).call()
.then((response) => {
file_counter++;
return response.remaining_number_of_files > 0
? getProm()
: response
});
return getProm();
});
}, Promise.resolve())
}).catch(function(error) {
console.log('Got this error:' + error);
});
使用 async
/await
代码可能 很多 更容易阅读和理解,但是:
await demo_schema_download.call();
const import_files_download = [];
for (const step of fake_data.steps_to_import) {
let response;
let file_counter = 1;
do {
response = await download_demo_file({
demo_handle: 'demo-2',
step,
file_counter
}).call();
file_counter++;
} while (response.remaining_number_of_files > 0);
import_files_download.push(response); // is this needed?
}
return import_files_download; // is this needed?
捕获异步函数的使用者。