js的promise如何递归调用?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js的promise如何递归调用?相关的知识,希望对你有一定的参考价值。
递归是Promise机制的核心之一:then方法返回另一个promise2,其内容是由回调方法的返回值决定的;回调中可以返回promise3,使外面的promise2的内容成为promise3的内容,也就是说,如果要用promise来递归,只需要在then回调中返回递归promise即可举例如下:
//假如用jQuery
function get(url, p)
return $.get(url + "?page=" + p)
.then(function(data)
if(!data.list.length) //递归结束条件
return [];
return get(url, p+1)//递归调用
.then(function(nextList)
return [].concat(data.list, nextList);//合并递归内容
);
);
get("urlurl", 1).then(function(list)
console.log(list);//your full list is here
); 参考技术A 好问题,正中Promise机制的核心之一:then方法返回另一个promise2,其内容是由回调方法的返回值决定的;回调中可以返回promise3,使外面的promise2的内容成为promise3的内容
也就是说,如果要用promise来递归,只需要在then回调中返回递归promise即可。 参考技术B 一.定义 promise是对异步编程的一种抽象。它是一个代理对象,代表一个必须进行异步处理的函数返回的值或抛出的异常。也就是说promise对象代表了一个异步操作
Node.js / Javascript 多重递归承诺在视图中返回
【中文标题】Node.js / Javascript 多重递归承诺在视图中返回【英文标题】:Node.js / Javascript multiple recursive promise to return at view 【发布时间】:2019-12-19 08:32:13 【问题描述】:我目前有一个包含多个递归调用的算法,我希望每个人都返回该算法以巩固我的结果
问题是递归调用太多,我不再知道如何返回我的合并结果
我尝试在每个promise的末尾通过结果数检查promise的数量来做出promise.all,但是我得到的结果表明我正在做出几个http响应
在这个版本中,我在所有承诺执行或添加到我的承诺列表之前返回结果。
var https = require('https');
var moment = require('moment');
app.get('/detail/:issue', function (req, res)
var promises = [];
var jsonResult =
total:
daysSpent: 0,
daysEstimated: 0,
daysRemaining: 0,
cost: 0
,
issues:
;
var getIssue = function (key)
/**
* Récupération des imputations par projet.
*/
promises.push(new Promise(function (resolve)
var options = Object.assign(, app.locals.data.jira);
options.path = "/jira/rest/api/2/issue/issueKey"
.replace('issueKey', key);
https.request(options, (resp) =>
let body = '';
resp.on('data', (chunk) =>
body += chunk;
);
resp.on('end', () =>
var issue = JSON.parse(body);
if (issue.fields.issuetype.name == 'Epic')
getEpic(issue.key);
else
jsonResult.issues[issue.key] =
key: issue.key,
numberTicket: issue.fields.customfield_10267 === null ? "-" : issue.fields.customfield_10267,
icon: issue.fields.issuetype.iconUrl,
name: issue.fields.summary,
status: issue.fields.status.name,
daysEstimated: issue.fields.issuetype.subtask ? (((issue.fields.timeoriginalestimate || 0) / 3600) / 7) : ((issue.fields.customfield_11901 || 0) / 7),
daysRemaining: issue.fields.issuetype.subtask ? (((issue.fields.timeoriginalestimate || 0) / 3600) / 7) : ((issue.fields.customfield_11901 || 0) / 7),
hoursSpent: 0,
daysSpent: 0,
cost: 0,
parent: issue.fields.parent === undefined ? issue.key : issue.fields.parent.key,
detail: ,
subtask: issue.fields.issuetype.subtask,
worklog: issue.fields.worklog.total != 0
jsonResult.total.daysEstimated += ((issue.fields.customfield_11901 || 0) / 7);
jsonResult.total.daysRemaining += ((issue.fields.customfield_11901 || 0) / 7);
if (issue.fields.subtasks != false)
for (let e in issue.fields.subtasks)
e = issue.fields.subtasks[e];
getIssue(e.key);
;
if (issue.fields.worklog.total != 0)
getWorklog(issue.key);
resolve();
);
).on("error", (e) =>
console.log("Error: " + e.message);
).end();
));
var getEpic = function (key)
/**
* Récupération des imputations par projet.
*/
promises.push(new Promise(function (resolve)
var postData = JSON.stringify(
"jql": "'Epic link' = issueKey"
.replace('issueKey', key),
"maxResults": -1,
"fields": [
"issuekey"
]
);
var options = Object.assign(, app.locals.data.jira);
options.path = "/jira/rest/api/2/search";
options.method = 'POST';
options.headers =
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
"Content-Length": Buffer.byteLength(postData)
;
var req = https.request(options, (resp) =>
let body = '';
resp.on('data', (chunk) =>
body += chunk;
);
resp.on('end', () =>
var issues = JSON.parse(body).issues;
for (let issue in JSON.parse(body).issues)
issue = issues[issue];
getIssue(issue.key);
;
resolve();
);
).on("error", (e) =>
console.log("Error: " + e.message);
);
req.write(postData);
req.end();
));
var getWorklog = function (key)
/**
* Récupération des imputations par projet.
*/
promises.push(new Promise(function (resolve)
var options = Object.assign(, app.locals.data.jira);
options.path = "/jira/rest/api/2/issue/issueKey/worklog"
.replace('issueKey', key);
https.request(options, (resp) =>
let body = '';
resp.on('data', (chunk) =>
body += chunk;
);
resp.on('end', () =>
var worklogs = JSON.parse(body).worklogs;
for (let e in worklogs)
e = worklogs[e];
if (jsonResult.issues[key].detail[e.author.key] == undefined)
jsonResult.issues[key].detail[e.author.key] =
name: e.author.displayName,
hoursSpent: 0,
daysSpent: 0,
cost: 0
jsonResult.issues[key].hoursSpent += e.timeSpentSeconds / 3600;
jsonResult.issues[key].detail[e.author.key].hoursSpent += e.timeSpentSeconds / 3600;
if (app.locals.data.scr[moment(e.started).format("Y")] !== undefined && app.locals.data.scr[moment(e.started).format("Y")][e.author.emailAddress] !== undefined)
var time = (e.timeSpentSeconds / 3600) / app.locals.data.scr[moment(e.started).format("Y")][e.author.emailAddress].modality;
var cost = time * app.locals.data.scr[moment(e.started).format("Y")][e.author.emailAddress].scr;
jsonResult.issues[key].detail[e.author.key].daysSpent += time;
jsonResult.issues[key].detail[e.author.key].cost += cost;
jsonResult.issues[key].daysSpent += time;
jsonResult.issues[key].cost += cost;
jsonResult.issues[key].daysRemaining -= time;
;
resolve();
);
).on("error", (e) =>
console.log("Error: " + e.message);
).end();
));
getIssue(req.params.issue);
Promise.all(promises).then(function ()
res.json(jsonResult);
);
);
【问题讨论】:
递归对于某些问题可能是一种很好的技术;在这种情况下,它增加了太多的认知开销(这意味着它很难跟踪程序执行)。您是否尝试过迭代方法?什么条件终止递归?将所有这些函数拆分为单独的文件也很有用。 我建议你使用async/await
(developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) 而不是promise。这将允许您编写函数调用,就好像它们是同步函数一样
不可能进行迭代,因为我事先不知道必须处理的元素数量。我处理多达 3 个层次级别的数据,其中第 2 级可以有 X 个子级。我将再次尝试等待/异步解决方案,这是我认为我可以做到的一种方法,但 nodeJS 告诉我我不明白
【参考方案1】:
我的问题已经解决了!
var https = require('https');
var moment = require('moment');
app.get('/detail/:issue', function (req, res)
var jsonResult =
total:
daysSpent: 0,
daysEstimated: 0,
daysRemaining: 0,
cost: 0
,
issues:
;
var getIssue = function (key)
/**
* Récupération des imputations par projet.
*/
return new Promise(function (resolve)
var options = Object.assign(, app.locals.data.jira);
options.path = "/jira/rest/api/2/issue/issueKey"
.replace('issueKey', key);
https.request(options, (resp) =>
let body = '';
resp.on('data', (chunk) =>
body += chunk;
);
resp.on('end', () =>
var promises = [];
var issue = JSON.parse(body);
if (issue.fields.issuetype.name == 'Epic')
promises.push(getEpic(issue.key));
else
jsonResult.issues[issue.key] =
key: issue.key,
numberTicket: issue.fields.customfield_10267 === null ? "-" : issue.fields.customfield_10267,
icon: issue.fields.issuetype.iconUrl,
name: issue.fields.summary,
status: issue.fields.status.name,
daysEstimated: issue.fields.issuetype.subtask ? (((issue.fields.timeoriginalestimate || 0) / 3600) / 7) : ((issue.fields.customfield_11901 || 0) / 7),
daysRemaining: issue.fields.issuetype.subtask ? (((issue.fields.timeoriginalestimate || 0) / 3600) / 7) : ((issue.fields.customfield_11901 || 0) / 7),
hoursSpent: 0,
daysSpent: 0,
cost: 0,
parent: issue.fields.parent === undefined ? issue.key : issue.fields.parent.key,
detail: ,
subtask: issue.fields.issuetype.subtask,
worklog: issue.fields.worklog.total != 0
jsonResult.total.daysEstimated += ((issue.fields.customfield_11901 || 0) / 7);
jsonResult.total.daysRemaining += ((issue.fields.customfield_11901 || 0) / 7);
if (issue.fields.worklog.total != 0)
promises.push(getWorklog(issue.key));
if (issue.fields.subtasks != false)
for (let e in issue.fields.subtasks)
e = issue.fields.subtasks[e];
promises.push(getIssue(e.key));
;
Promise.all(promises).then(function ()
resolve();
);
);
).on("error", (e) =>
console.log("Error: " + e.message);
).end();
);
var getEpic = function (key)
/**
* Récupération des imputations par projet.
*/
return new Promise(function (resolve)
var postData = JSON.stringify(
"jql": "'Epic link' = issueKey"
.replace('issueKey', key),
"maxResults": -1,
"fields": [
"issuekey"
]
);
var options = Object.assign(, app.locals.data.jira);
options.path = "/jira/rest/api/2/search";
options.method = 'POST';
options.headers =
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
"Content-Length": Buffer.byteLength(postData)
;
var req = https.request(options, (resp) =>
let body = '';
resp.on('data', (chunk) =>
body += chunk;
);
resp.on('end', () =>
var promises = [];
var issues = JSON.parse(body).issues;
for (let issue in JSON.parse(body).issues)
issue = issues[issue];
promises.push(getIssue(issue.key));
;
Promise.all(promises).then(function ()
resolve();
);
);
).on("error", (e) =>
console.log("Error: " + e.message);
);
req.write(postData);
req.end();
);
var getWorklog = function (key)
/**
* Récupération des imputations par projet.
*/
return new Promise(function (resolve)
var options = Object.assign(, app.locals.data.jira);
options.path = "/jira/rest/api/2/issue/issueKey/worklog"
.replace('issueKey', key);
https.request(options, (resp) =>
let body = '';
resp.on('data', (chunk) =>
body += chunk;
);
resp.on('end', () =>
var worklogs = JSON.parse(body).worklogs;
for (let e in worklogs)
e = worklogs[e];
if (jsonResult.issues[key].detail[e.author.key] == undefined)
jsonResult.issues[key].detail[e.author.key] =
name: e.author.displayName,
hoursSpent: 0,
daysSpent: 0,
cost: 0
jsonResult.issues[key].hoursSpent += e.timeSpentSeconds / 3600;
jsonResult.issues[key].detail[e.author.key].hoursSpent += e.timeSpentSeconds / 3600;
if (app.locals.data.scr[moment(e.started).format("Y")] !== undefined && app.locals.data.scr[moment(e.started).format("Y")][e.author.emailAddress] !== undefined)
var time = (e.timeSpentSeconds / 3600) / app.locals.data.scr[moment(e.started).format("Y")][e.author.emailAddress].modality;
var cost = time * app.locals.data.scr[moment(e.started).format("Y")][e.author.emailAddress].scr;
jsonResult.issues[key].detail[e.author.key].daysSpent += time;
jsonResult.issues[key].detail[e.author.key].cost += cost;
jsonResult.issues[key].daysSpent += time;
jsonResult.issues[key].cost += cost;
jsonResult.issues[key].daysRemaining -= time;
if (jsonResult.issues[key].subtask)
jsonResult.issues[jsonResult.issues[key].parent].hoursSpent += e.timeSpentSeconds / 3600;
jsonResult.issues[jsonResult.issues[key].parent].daysSpent += time;
jsonResult.issues[jsonResult.issues[key].parent].cost += cost;
jsonResult.issues[jsonResult.issues[key].parent].daysRemaining -= time;
jsonResult.total.daysSpent += time;
jsonResult.total.cost += cost;
jsonResult.total.daysRemaining -= time;
;
resolve();
);
).on("error", (e) =>
console.log("Error: " + e.message);
).end();
);
getIssue(req.params.issue).then(function ()
res.json(jsonResult);
);
);
每个方法都等待其所有子方法结束才能结束
【讨论】:
以上是关于js的promise如何递归调用?的主要内容,如果未能解决你的问题,请参考以下文章