@wulfklaue
Oh! ECMAScript2015, ECMAScript7 and nodejs are very popular now. I think you
just not understand JavaScript. You should have a look at:
import fs from "fs";
const stateNone = 0;
const stateRead = 1;
const stateWrite = 2;
class IO {
constructor() {
this.state = stateNone;
}
async read(path) {
var io = this;
return new Promise(function (complete, fail) {
io.state = stateRead;
fs.readFile(path, function (err, data) {
if (err) {
fail(err);
} else {
io.state = stateNone;
complete(data);
}
})
})
}
async write(path, data) {
var io = this;
return new Promise(function (complete, fail) {
io.state = stateWrite;
fs.writeFile(path, data, function (err) {
if (err) {
fail(err);
} else {
io.state = stateNone;
complete();
}
})
})
}
}
(async function main() {
var io = new IO();
try {
var data = await io.read("/home/user/test1.txt");
await io.write("/home/user/test2.txt", data);
} catch (e) {
console.log("Error: ", e);
console.log("IO state: ", io.state);
}
} ());