Api how to use File::read?


#1

Hi everybody,

I need some help about the read method from File class. Basically I just want load a file, read the content and put the content in a new file.

Hereafter my code :

fetch() {
project_path = atom.project.getPaths();
console.log(project_path[0]);

let path2 = project_path[0] + "\\template\\template.md"
console.log(path2);
const myFile2 = new File(path2);
var text2 = myFile2.read(true);
console.log(text2);

}

text2 is not a string with the file content but a promise, how can I get the file content please?

Thank you in advance for your answers!


#2

You can make your function async and use await, or add a then method to use thenresolved value. Look up JS Promises for more on how to use them.


#3

MDN has a great introduction to Promises. In this case, Node has already created the Promise for you, so once you retrieve it from myFile2.read(), you just have to tell the Promise what to do when it resolves:

myFile2.read(true).then( (text2) => {
 console.log(text2)
}