#javascript #reactjs
#javascript #reactjs
Вопрос:
Я получаю этот массив с обещанием перед этим, которое работает нормально, но когда он выдает массив по отдельности, когда я утешаю обещания, он работает нормально, но когда дело доходит до конечного результата promise all, он дает неопределенный
const prom = (leader) => {
try {
fetch(
"http://example/api/junk?"
new URLSearchParams({
//the elements of array goes here
id: leader
}),
{
method: "GET",
headers: {
"Content-Type": "application/json",
token: token.id
}
}
)
.then((res) => {
//the response is converted to json
return res.json();
})
.then((value) => {
//iam returning the value here to the promise
return value;
});
} catch (error) {
return error;
}
};
//user.leads is an array of 14 prameters
Promise.all(
user.leads.map((lead) => {
//return a new promise
return new Promise((resolve, reject) => resolve(prom(lead)));
})
)
//the promise all output
.then((value) => console.log(value));
Он возвращает (14) [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
Комментарии:
1. Почему бы и нет
Promise.all(user.leads.map(prom).then(...
?fetch
возвращает обещание, так что, возможно, промежуточное обещание не соответствует всему этому.2. Вау …! это действительно сработало, спасибо большое, я бился головой об это последние 5 часов … спасибо за повтор…