-2

I am trying to convert the froogaloop javascript api into a promisified API calls,

here is my code

FroogaLoopPlayer.promisify = (player) => {
  //player.on = player.addEvent;

  _.each(functionNames, (fn) => {
    _.each(fn, (value, key) => {
      player[key] = () => {
        var data;
        return new Bluebird(resolve => {
          player.api(value, (d) => {
            console.log(d);
            data = d;
            console.log(data);
          });

          resolve(data);
        });
      };
    });
  });

  return player;
}; 

the problem is, the data i get is always undefined. What I could be doing wrong?. I have already tried this, which i suppose is the correct way.

return new Bluebird(resolve => {
  player.api(value, resolve);
});
Yousuf Jawwad
  • 3,037
  • 7
  • 33
  • 60

1 Answers1

0

Don't put your resolve() after the call to the asynchronous function (where data is undefined of course), but in the callback:

return new Bluebird(resolve => {
  player.api(value, (d) => {
    resolve(d);
  });
}).then(data => {
  console.log(data);
  return data;
});

You can also use just player.api(value, resolve).

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Maybe `d` is nothing either? How do you call the function? – Bergi Apr 27 '16 at 13:46
  • like this ```player.getCurrentTime().then((time) => { console.log($scope.dip); $scope.dip.time = time; });```, and no, when i do ```console.log(d)``` .. it returns me a value. – Yousuf Jawwad Apr 27 '16 at 13:54
  • also, when i do how you tell me to do it, console.log does not get executed. the second argument disappears somewhere) – Yousuf Jawwad Apr 27 '16 at 13:55
  • So the callback does get executed, but `time` is undefined? – Bergi Apr 27 '16 at 13:56
  • when i do, ```resolve(d)```, like you said, the callback does not get executed. – Yousuf Jawwad Apr 27 '16 at 13:59
  • Does the `(d) => {resolve(d);}` callback get executed at all? – Bergi Apr 27 '16 at 14:08
  • no. .. when i do ```(d) => { console.log(d); resolve(d); }```, i don't get anything in console. only when i resolve the promise outside the function call, we get something. – Yousuf Jawwad Apr 27 '16 at 14:22
  • i also tried doing resolve(player.api()); .. when doesn't get me anything. – Yousuf Jawwad Apr 27 '16 at 14:23
  • Well then you have to blame `player.api` not calling its callback, not the promises. Is it an asynchronous method at all? Do you have a link to its docs? – Bergi Apr 27 '16 at 15:07
  • this is the froogaloop sdk link https://github.com/vimeo/player-api/blob/master/javascript/froogaloop.js and these are the docs https://developer.vimeo.com/player/js-api – Yousuf Jawwad Apr 27 '16 at 15:26
  • @mouse: It appears that `void` "methods" don't call their callback back - as they don't "return" anything, and they're not even really asynchronous. – Bergi Apr 27 '16 at 15:32
  • 1
    thankyou @Bergi .. it appears the problem is with froogaloop library. – Yousuf Jawwad Apr 29 '16 at 20:30