Probé el Promise.all habitual, pero aparece un error Json circular. Tengo las siguientes promesas en mi consola
all [ Promise { <pending> }, Promise { <pending> } ] all [ Promise { <pending> } ] all [ Promise { <pending> } ] all [ Promise { <pending> } ]Con el siguiente código
const needle = require("needle"); const token = process.env.BEARER_TOKEN_TWITTER; const endpointUrl = "https://api.twitter.com/2/tweets/search/recent"; export default async function login(req, res) { try { const all = req.body.map((item) => { const params = { query: `"$${item.title}" from:${item.twitter}`, }; return needle("get", endpointUrl, params, { headers: { "User-Agent": "v2FullArchiveJS", authorization: `Bearer ${token}`, }, }).catch((err) => { console.warn(err); }); }); console.log("all", all); // Update console.log("allll", await Promise.all(all)); const result = await Promise.all(all); res.status(200).json({ authenticated: true, result: result }); } catch (error) { res.status(500).json({ error: error.message }); } }He leído lo siguiente sin suerte.
¿Cómo hacer promise.all para una matriz de una matriz de promesas?
Llamada frontal
const fetchData = async () => { const response = newArray.map( async (question) => { console.log("question", question.data); return await fetchTweets(question.data); } ); console.log("response", response); const promise4all = await Promise.all( response ); console.log("promise4all", promise4all); // setTweets(await Promise.all(response)); };Tuve que hacer lo siguiente
const needle = require("needle"); const token = process.env.BEARER_TOKEN_TWITTER; const endpointUrl = "https://api.twitter.com/2/tweets/search/recent"; export default async function login(req, res) { try { const all = req.body.map((item) => { const params = { query: `"$${item.title}" from:${item.twitter}`, }; return needle("get", endpointUrl, params, { headers: { "User-Agent": "v2FullArchiveJS", authorization: `Bearer ${token}`, }, }).then(function (response) { return response.body; }); }); const result = Promise.all(all); res.status(200).json({ authenticated: true, result: await result }); } catch (error) { res.status(500).json({ error: error.message }); } }Interfaz
useEffect(() => { // declare the async data fetching function const fetchData = async () => { const response = newArray.map( async (question) => { console.log("question", question.data); return await fetchTweets(question.data); } ); setTweets(await Promise.all(response)); }; if (data) { fetchData(); } }, [data]);