return storage.read content

So the documentation says https://www.fusetools.com/learn/fusejs#storage-read-read and it’s working fine, the console prints out the content. But, it seems that i can’t get the content out of this function.

Just a basic example:

var test = Observable({'foo': 'bar'});               //start with one element
storage.write('foobar', 'something');                //write something into file
storage.read('foobar').then(function(content) {      //read out something
    test.add({'foo2': content});                     //add something as second element
});
console.log(test.length);                            //count elements

I expected this to return 2 cause i start with one element, then add another one and should end up with 2, but it always prints out 1. That can only mean, that test is not available or can’t be modified inside the storage.read() function for some reason.

Is there any chance to read a file and work with the content outside of the storage.read() function?

Since these methods is async, they are not done with write/read before you print out the length. You can either do it sync or async like this:

var test = Observable({'foo': 'bar'});
console.log(test.length);

// Blocking
storage.writeSync('foobar', 'something');
var content = storage.readSync('foobar');
test.add({'foo2': content});
console.log(test.length);

// Async
storage.write('foobar', 'something').then(function() {
  storage.read('foobar').then(function(content) {
    test.add({'foo3': content});
    console.log(test.length);
  });
});

Thanks man! That really helps a lot. Very appreciated.