Processing
 

Safari not calling Promise.then in twitter createWidget

26/08/2016 09:00#1

Joined at: 10 months ago

Post: 22

Thank: 0

Thanked: 0

I'm using Twitter's twttr.widgets.createTweet to render a list of tweets to the page, by ID (initialTweets is a json of Tweet IDs).

    var initialTweets = <?php echo json_encode($tweets); ?>;     $(window).load(function(){         if(initialTweets.length) {             for(i = 0; i < initialTweets.length; i++) {                 var data = initialTweets[i];                 var newTweet = document.createElement('div');                 twttr.widgets.createTweet(                     data, newTweet, {}                 ).then(function (el) {                     $('#tweets').prepend($(el));                     var tweets = $('#tweets > twitterwidget');                     console.log('Tweets: ' . tweets.length);                     if (tweets.length > max) {                         tweets.eq(max).nextAll().remove();                     }                 });             }         }     }); 

This works fine in Chrome 52, but isn't working in Safari - if I add some console logging, it seems that the .then function isn't being called. I know that the Tweet element is being created, as I can see the iframes being called in network inspector.

26/08/2016 19:53Top#2

Joined at: 10 months ago

Post: 3

Thank: 0

Thanked: 0

Mmm, hard to say why Safari should be problematic.

Here's something which may just be good for diagnosis or may offer a permanent solution. The idea is to prepend a target div to #tweets in advance of calling twttr.widgets.createTweet(), offering the possibility of :

  • displaying a "loading..." message for each tweet.
  • displaying an error message message for each tweet.
  • ensuring that the tweets will be displayed in the same order as the original initialTweets array, which is not guaranteed in the current code.
$(window).load(function() {     var initialTweets = <?php echo json_encode($tweets); ?>,         $tweets = $('#tweets'),         loadingMessage = 'loading...';     initialTweets.forEach(function(tweetID) {         var $div = $('<div/>').text(loadingMessage).prependTo($tweets); // create and prepend a target div         return twttr.widgets.createTweet(tweetID, $div.get(0), {}).then(function(el) {             $(el).unwrap(); // remove the prepended target div, leaving the twitter widget in place             var $widgets = $tweets.children('twitterwidget');             if ($widgets.length > max) {                 $widgets.eq(max).nextAll().remove();             }         }, function(error) {             $div.text(error.message); // for testing/diagnosis             // $div.remove(); // for live environment             throw error;         });     }); }); 
Similar articles

How do twitter giveaways work?

8 months ago - Reply: 2 - Views: 126

How may I get genuine followers on Twitter?

3 years ago - Reply: 33 - Views: 1,828

Login to Twitter sometime fail

6 months ago - Reply: 1 - Views: 23

Twitter doesn't accept PHP's cURL, but Linux'

1 year ago - Reply: 2 - Views: 90

Twitter OAuth on iOS

5 months ago - Reply: 1 - Views: 92

why am i getting an error on .verifyCredentials()

5 months ago - Reply: 1 - Views: 35

TwitteR setup_twitter_oauth() gives an error

5 months ago - Reply: 1 - Views: 29

Tweepy - Getting the author of a retweeted tweet

5 months ago - Reply: 0 - Views: 4

Retrieving data from twitter with JavaScript

5 months ago - Reply: 0 - Views: 2

Combined social media feed returning the wrong data

5 months ago - Reply: 0 - Views: 0

Django and Twitter request_token

5 months ago - Reply: 0 - Views: 0

Python HMAC hashed value encoding to base64

5 months ago - Reply: 1 - Views: 32

At the rate symbol in facebook

5 months ago - Reply: 0 - Views: 1

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re