0

I'm using the following code to return my result but I always get undefined however I get the data I want in my console log. I believe this has something to do with promises, but I'm not sure how to deal with it.

var blobUtil = require('blob-util');
var mystring = "Hello World!";
var myblob = new Blob([mystring], {
    type: 'text/plain'
});

this.BlobData(myblob);

function readBlob(blob) {

    blobUtil.blobToBinaryString(blob)
      .then(function (result) {
      return result;
    });
};

function BlobData(blob) {

    console.log(readBlob(blob));

}
Nelson Silva
  • 409
  • 7
  • 20
  • What exactly is `undefined` here? – Teemu Apr 27 '18 at 10:18
  • @Teemu updated my question. I get undefined at my BlobData's log. – Nelson Silva Apr 27 '18 at 10:29
  • 1
    `readBlob` does not `return` anything, the `return` in the `then` callback only returns from that callback. You need to return the promise, and wait for it before logging its result: `blobUtil.blobToBinaryString(blob).then(console.log)` – Bergi Apr 27 '18 at 10:32
  • @Bergi yes, that way I get the log of the data I want but how can I return that value so I can catch it in another method? – Nelson Silva Apr 27 '18 at 10:37
  • 1
    You cannot, the data is not immediately available. The other method will need to be called as the `then` handler. – Bergi Apr 27 '18 at 10:43
  • @Bergi can you provide me an example? Already looked into the answer you marked and I'm not getting the idea... – Nelson Silva Apr 27 '18 at 10:54
  • 1
    @NelsonSilva I already gave an example, where `console.log` is the "other method". Maybe you can [edit] your question with a more complete example. – Bergi Apr 27 '18 at 12:07

0 Answers0