Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

76
Views
Properly resolving multiple twttr.widgets.createTweet promises

I'm trying to add multiple tweets to a page as it is paginated. However, I cannot seem to get widget.createTweet promises to resolve in order to add their element to the page.

Here is the logic from my reduced Codepen example.

 // increment current page number
 // create next set of tweets
 // append tweet elems to masonry container
 async function paginate() {
     let current = page;
     page = page + 3;
     var tweetIDs = source.slice(current, page);

     // create tweets/widget promises
     const tweetPromises = tweetIDs.map(async id => {
         // create grid element
         let gridEl = document.createElement("div");
         // set masonry class
         gridEl.setAttribute("class", "grid__item");
         // twitter widget.js - get promise
         const response = twttr.widgets.createTweet(id, gridEl, {
             theme: colorScheme,
             dnt: true
         });
         return response;
     });

     // Promise.all over loops
     let results = Promise.all(tweetPromises).then((m) => {
         console.log("then", m); // never happens
     })
     .catch(e => {
         console.log("catch", e.reason);
     });

     console.log(tweetIDs, tweetPromises, results, page);
     // need the resolved tweets first!
     // msnry.appended(newTweetElms);
     // msnry.addItems(newTweetElms);
     // msnry.layout();
 }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This isn't a problem with your use of promises; Twitter just won't resolve the promises unless the element you pass into createWidget is added to the DOM.

let gridEl = document.createElement("div");
document.querySelector("#twitter").appendChild(gridEl);

Other than that, your code looks fine, aside from your use of then/catch in an async function. (It works, but it can be confusing, because you've got two different styles of asynchronous behavior.) This, for instance, makes it less obvious that your final diagnostic console.log will always show a Promise for results. If you were to log await results (and drop your "never happens" then and catch logging) your final console.log would show your array of actual elements.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!