RickyAYoder RickyAYoder - 1 year ago 83
Javascript Question

How to return value from a callback within a function?

I'm trying to make a simple function to take a JavaScript

and "convert" it into a JavaScript
object. I have the basic function here:

function fileEntrytoFile(e){
var f = 0;

f = file;
return f;

is still 0 when it is returned. How can I return the
object that is being generated by the callback function within?

Answer Source

You will need to return a promise, since FileEntry.file is asynchronous:

function fileEntrytoFile(e){
  return new Promise((resolve, reject) => e.file(resolve, reject));

fileEntrytoFile(fileEntry) . then(file => console.log(file.name));

The above uses ES6 arrow functions and ES6 promises.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download