Я думаю, что ошибка не в коде, который вы указали;это определенно не ошибка рекурсии.Я могу заставить его работать с вашим точным кодом, подделав службы, которые вы вызываете.
Дважды проверьте глупые ошибки, такие как опечатки.(Ваш сервис возвращает statuscode
или statusCode
? И т. Д.)
const AuthClient = {credentials: {getToken: () =>
Promise.resolve({accessToken: 'foobar'})
}}
const getTypes = (typeid) =>
new Promise((resolve, _) => resolve(({
roottypeid: `[{"id": "type1"}, {"id": "type2"}]`,
type1: `[{"id": "type11"}, {"id": "type12"}]`,
type11: `[{"id": "type111"}]`,
type2: `[{"id": "type21"}, {"id": "type22"}]`
})[typeid] || `[]`)
)
const request = (options, fn) => setTimeout(
() => fn (
null,
{statuscode: 200},
`fake results for ${JSON.stringify(options.body)}`
),
10
)
const url = 'http://example.com'
const roottypeid = 'roottypeid'
function func1(typeid) {
getTypes(typeid).then(function(responseData) { //getTypes returns all the child types of a given parent typeid
var types = JSON.parse(responseData);
AuthClient.credentials.getToken()
.then(function(token) {
types.forEach(function(element) {
var options = {
method: 'POST',
url: url,
qs: {
access_token: token.accessToken
},
headers: {
'content-type': 'application/json'
},
body: element,
json: true
}
request(options, function(error, response, body) {
if (error) throw new Error(error)
if (response.statuscode === 200) {
func1(element.id);
console.log(body);
}
});
});
})
.catch(function(err) {
console.log(err);
});
});
}
func1(roottypeid);