Extract dataURL from Javascript function











up vote
0
down vote

favorite
1












I have a function that aims to create a dataURL of a file using the FileReader. Considering this example.



toDataURL(url, callback){
var xhr = new XMLHttpRequest();
xhr.open('get', url);
xhr.responseType = 'blob';
xhr.onload = function(){
var fr = new FileReader();
var test = fr.onload = function(){
callback(this.result);
};

fr.readAsDataURL(xhr.response); // async call
};

xhr.send();
}


I want to extract the variable this.result in the inline function as a return variable. It has not acces from other function. How can I do it?










share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    I have a function that aims to create a dataURL of a file using the FileReader. Considering this example.



    toDataURL(url, callback){
    var xhr = new XMLHttpRequest();
    xhr.open('get', url);
    xhr.responseType = 'blob';
    xhr.onload = function(){
    var fr = new FileReader();
    var test = fr.onload = function(){
    callback(this.result);
    };

    fr.readAsDataURL(xhr.response); // async call
    };

    xhr.send();
    }


    I want to extract the variable this.result in the inline function as a return variable. It has not acces from other function. How can I do it?










    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I have a function that aims to create a dataURL of a file using the FileReader. Considering this example.



      toDataURL(url, callback){
      var xhr = new XMLHttpRequest();
      xhr.open('get', url);
      xhr.responseType = 'blob';
      xhr.onload = function(){
      var fr = new FileReader();
      var test = fr.onload = function(){
      callback(this.result);
      };

      fr.readAsDataURL(xhr.response); // async call
      };

      xhr.send();
      }


      I want to extract the variable this.result in the inline function as a return variable. It has not acces from other function. How can I do it?










      share|improve this question













      I have a function that aims to create a dataURL of a file using the FileReader. Considering this example.



      toDataURL(url, callback){
      var xhr = new XMLHttpRequest();
      xhr.open('get', url);
      xhr.responseType = 'blob';
      xhr.onload = function(){
      var fr = new FileReader();
      var test = fr.onload = function(){
      callback(this.result);
      };

      fr.readAsDataURL(xhr.response); // async call
      };

      xhr.send();
      }


      I want to extract the variable this.result in the inline function as a return variable. It has not acces from other function. How can I do it?







      javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 15:33









      Kuler Can

      61




      61
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Basicaly, what you want to do is return a value from an async function, you can do this with Promises, and optionally async/await.



          Check this out:






          function toDataURL(url, callback) {
          return new Promise( function(resolve, reject) {

          var xhr = new XMLHttpRequest();
          xhr.open('get', url);
          xhr.responseType = 'blob';
          xhr.onload = function() {
          var fr = new FileReader();
          var test = fr.onload = function() {
          resolve(this.result)
          callback(this.result);
          };

          fr.readAsDataURL(xhr.response); // async call
          };

          xhr.onerror = function() {
          reject('Error);
          }

          xhr.send();
          });
          }

          const result = await toDataURL('some url');

          // or

          toDataURL('some url').then((result) => {
          // do something with the result
          });








          share|improve this answer




























            up vote
            0
            down vote













            You could try to use Promise, like this:



            toDataURL(url){
            var promise = new Promise(function (extract) {
            var xhr = new XMLHttpRequest();
            xhr.open('get', url);
            xhr.responseType = 'blob';
            xhr.onload = function(){
            var fr = new FileReader();
            var test = fr.onload = function(){
            extract(this.result);
            };

            fr.readAsDataURL(xhr.response); // async call
            };

            xhr.send();
            });

            return promise;
            }

            (async function () {
            var result = await toDataURL('/someurl');

            console.log(result);
            }());


            Inside the toDataURL function, we create a promise. When returning the result, we just need to put it inside extract method.



            Then, we create an asynchronous function to use await keyword to get the result directly.






            share|improve this answer





















            • I have an other questio. For Typescrippt, how can I change the global variable in the promise function?
              – Kuler Can
              Nov 27 at 9:20











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415469%2fextract-dataurl-from-javascript-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Basicaly, what you want to do is return a value from an async function, you can do this with Promises, and optionally async/await.



            Check this out:






            function toDataURL(url, callback) {
            return new Promise( function(resolve, reject) {

            var xhr = new XMLHttpRequest();
            xhr.open('get', url);
            xhr.responseType = 'blob';
            xhr.onload = function() {
            var fr = new FileReader();
            var test = fr.onload = function() {
            resolve(this.result)
            callback(this.result);
            };

            fr.readAsDataURL(xhr.response); // async call
            };

            xhr.onerror = function() {
            reject('Error);
            }

            xhr.send();
            });
            }

            const result = await toDataURL('some url');

            // or

            toDataURL('some url').then((result) => {
            // do something with the result
            });








            share|improve this answer

























              up vote
              0
              down vote













              Basicaly, what you want to do is return a value from an async function, you can do this with Promises, and optionally async/await.



              Check this out:






              function toDataURL(url, callback) {
              return new Promise( function(resolve, reject) {

              var xhr = new XMLHttpRequest();
              xhr.open('get', url);
              xhr.responseType = 'blob';
              xhr.onload = function() {
              var fr = new FileReader();
              var test = fr.onload = function() {
              resolve(this.result)
              callback(this.result);
              };

              fr.readAsDataURL(xhr.response); // async call
              };

              xhr.onerror = function() {
              reject('Error);
              }

              xhr.send();
              });
              }

              const result = await toDataURL('some url');

              // or

              toDataURL('some url').then((result) => {
              // do something with the result
              });








              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Basicaly, what you want to do is return a value from an async function, you can do this with Promises, and optionally async/await.



                Check this out:






                function toDataURL(url, callback) {
                return new Promise( function(resolve, reject) {

                var xhr = new XMLHttpRequest();
                xhr.open('get', url);
                xhr.responseType = 'blob';
                xhr.onload = function() {
                var fr = new FileReader();
                var test = fr.onload = function() {
                resolve(this.result)
                callback(this.result);
                };

                fr.readAsDataURL(xhr.response); // async call
                };

                xhr.onerror = function() {
                reject('Error);
                }

                xhr.send();
                });
                }

                const result = await toDataURL('some url');

                // or

                toDataURL('some url').then((result) => {
                // do something with the result
                });








                share|improve this answer












                Basicaly, what you want to do is return a value from an async function, you can do this with Promises, and optionally async/await.



                Check this out:






                function toDataURL(url, callback) {
                return new Promise( function(resolve, reject) {

                var xhr = new XMLHttpRequest();
                xhr.open('get', url);
                xhr.responseType = 'blob';
                xhr.onload = function() {
                var fr = new FileReader();
                var test = fr.onload = function() {
                resolve(this.result)
                callback(this.result);
                };

                fr.readAsDataURL(xhr.response); // async call
                };

                xhr.onerror = function() {
                reject('Error);
                }

                xhr.send();
                });
                }

                const result = await toDataURL('some url');

                // or

                toDataURL('some url').then((result) => {
                // do something with the result
                });








                function toDataURL(url, callback) {
                return new Promise( function(resolve, reject) {

                var xhr = new XMLHttpRequest();
                xhr.open('get', url);
                xhr.responseType = 'blob';
                xhr.onload = function() {
                var fr = new FileReader();
                var test = fr.onload = function() {
                resolve(this.result)
                callback(this.result);
                };

                fr.readAsDataURL(xhr.response); // async call
                };

                xhr.onerror = function() {
                reject('Error);
                }

                xhr.send();
                });
                }

                const result = await toDataURL('some url');

                // or

                toDataURL('some url').then((result) => {
                // do something with the result
                });





                function toDataURL(url, callback) {
                return new Promise( function(resolve, reject) {

                var xhr = new XMLHttpRequest();
                xhr.open('get', url);
                xhr.responseType = 'blob';
                xhr.onload = function() {
                var fr = new FileReader();
                var test = fr.onload = function() {
                resolve(this.result)
                callback(this.result);
                };

                fr.readAsDataURL(xhr.response); // async call
                };

                xhr.onerror = function() {
                reject('Error);
                }

                xhr.send();
                });
                }

                const result = await toDataURL('some url');

                // or

                toDataURL('some url').then((result) => {
                // do something with the result
                });






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 15:39









                rubentd

                986821




                986821
























                    up vote
                    0
                    down vote













                    You could try to use Promise, like this:



                    toDataURL(url){
                    var promise = new Promise(function (extract) {
                    var xhr = new XMLHttpRequest();
                    xhr.open('get', url);
                    xhr.responseType = 'blob';
                    xhr.onload = function(){
                    var fr = new FileReader();
                    var test = fr.onload = function(){
                    extract(this.result);
                    };

                    fr.readAsDataURL(xhr.response); // async call
                    };

                    xhr.send();
                    });

                    return promise;
                    }

                    (async function () {
                    var result = await toDataURL('/someurl');

                    console.log(result);
                    }());


                    Inside the toDataURL function, we create a promise. When returning the result, we just need to put it inside extract method.



                    Then, we create an asynchronous function to use await keyword to get the result directly.






                    share|improve this answer





















                    • I have an other questio. For Typescrippt, how can I change the global variable in the promise function?
                      – Kuler Can
                      Nov 27 at 9:20















                    up vote
                    0
                    down vote













                    You could try to use Promise, like this:



                    toDataURL(url){
                    var promise = new Promise(function (extract) {
                    var xhr = new XMLHttpRequest();
                    xhr.open('get', url);
                    xhr.responseType = 'blob';
                    xhr.onload = function(){
                    var fr = new FileReader();
                    var test = fr.onload = function(){
                    extract(this.result);
                    };

                    fr.readAsDataURL(xhr.response); // async call
                    };

                    xhr.send();
                    });

                    return promise;
                    }

                    (async function () {
                    var result = await toDataURL('/someurl');

                    console.log(result);
                    }());


                    Inside the toDataURL function, we create a promise. When returning the result, we just need to put it inside extract method.



                    Then, we create an asynchronous function to use await keyword to get the result directly.






                    share|improve this answer





















                    • I have an other questio. For Typescrippt, how can I change the global variable in the promise function?
                      – Kuler Can
                      Nov 27 at 9:20













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    You could try to use Promise, like this:



                    toDataURL(url){
                    var promise = new Promise(function (extract) {
                    var xhr = new XMLHttpRequest();
                    xhr.open('get', url);
                    xhr.responseType = 'blob';
                    xhr.onload = function(){
                    var fr = new FileReader();
                    var test = fr.onload = function(){
                    extract(this.result);
                    };

                    fr.readAsDataURL(xhr.response); // async call
                    };

                    xhr.send();
                    });

                    return promise;
                    }

                    (async function () {
                    var result = await toDataURL('/someurl');

                    console.log(result);
                    }());


                    Inside the toDataURL function, we create a promise. When returning the result, we just need to put it inside extract method.



                    Then, we create an asynchronous function to use await keyword to get the result directly.






                    share|improve this answer












                    You could try to use Promise, like this:



                    toDataURL(url){
                    var promise = new Promise(function (extract) {
                    var xhr = new XMLHttpRequest();
                    xhr.open('get', url);
                    xhr.responseType = 'blob';
                    xhr.onload = function(){
                    var fr = new FileReader();
                    var test = fr.onload = function(){
                    extract(this.result);
                    };

                    fr.readAsDataURL(xhr.response); // async call
                    };

                    xhr.send();
                    });

                    return promise;
                    }

                    (async function () {
                    var result = await toDataURL('/someurl');

                    console.log(result);
                    }());


                    Inside the toDataURL function, we create a promise. When returning the result, we just need to put it inside extract method.



                    Then, we create an asynchronous function to use await keyword to get the result directly.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 at 15:40









                    Foo

                    1




                    1












                    • I have an other questio. For Typescrippt, how can I change the global variable in the promise function?
                      – Kuler Can
                      Nov 27 at 9:20


















                    • I have an other questio. For Typescrippt, how can I change the global variable in the promise function?
                      – Kuler Can
                      Nov 27 at 9:20
















                    I have an other questio. For Typescrippt, how can I change the global variable in the promise function?
                    – Kuler Can
                    Nov 27 at 9:20




                    I have an other questio. For Typescrippt, how can I change the global variable in the promise function?
                    – Kuler Can
                    Nov 27 at 9:20


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415469%2fextract-dataurl-from-javascript-function%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...