Nikita Malyschkin
@nmalyschkin
Nikita Malyschkin
web developer by day
philosopher when drunk
Javascript / Python / C++ / more
Twitter / GitHub / Telegram
@nmalyschkin
import { readFile, writeFile } from "fs";
const main = () => {
// copy file by read and write
};
main();
import { readFile, writeFile } from "fs";
const main = () => {
readFile("./my/file");
};
main();
import { readFile, writeFile } from "fs";
const main = () => {
readFile("./my/file", (err, data) => {
/* do something here */
});
};
main();
import { readFile, writeFile } from "fs";
const main = () => {
readFile("./my/file", (err, data) => {
if (err) {
console.log(err);
return;
}
// write data to new file
});
};
main();
import { readFile, writeFile } from "fs";
const main = () => {
readFile("./my/file", (err, data) => {
if (err) {
console.log(err);
return;
}
writeFile("./my/fileCopy", data);
});
};
main();
import { readFile, writeFile } from "fs";
const main = () => {
readFile("./my/file", (err, data) => {
if (err) {
console.log(err);
return;
}
writeFile("./my/fileCopy", data, err => {
if (err) {
console.log(err);
return;
}
console.log("file copied");
});
});
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = () => {
// copy file by read and write
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = () => {
readFile("./my/file")
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = () => {
readFile("./my/file")
.then(data => {/* do something here */})
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = () => {
readFile("./my/file")
.then(data => writeFile("./my/fileCopy"))
.then(() => console.log("file copied"))
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = () => {
readFile("./my/file")
.then(data => writeFile("./my/fileCopy"))
.then(() => console.log("file copied"))
.catch(err => console.log(err));
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = () => {
readFile("./my/file")
.then(data => writeFile("./my/fileCopy"))
.then(() => console.log("file copied"))
.catch(err => console.log(err))
.finally(() => console.log("done"));
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = () => {
// copy file by read and write
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = async () => {
// copy file by read and write
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = async () => {
const data = await readFile("./my/file");
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = async () => {
const data = await readFile("./my/file");
await writeFile(".my/fileCopy", data);
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = async () => {
const data = await readFile("./my/file");
await writeFile(".my/fileCopy", data);
console.log("file copied");
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = async () => {
try {
const data = await readFile("./my/file");
await writeFile(".my/fileCopy", data);
console.log("file copied");
} catch (error) {
console.log(error);
}
console.log("done");
};
main();
.js
.mjs
const fs = require("fs")
import fs from "fs"
ES Modules
≠
Babel Modules
require
exports
module.exports
__filename
__dirname
(in modules)
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const main = async () => {
const data = await readFile("./my/file");
await writeFile(".my/fileCopy", data);
console.log("file copied");
};
main();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
(async () => {
const data = await readFile("./my/file");
await writeFile(".my/fileCopy", data);
console.log("file copied");
})();
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
// with top level await
const data = await readFile("./my/file");
await writeFile(".my/fileCopy", data);
console.log("file copied");
import fs from "fs";
import util from "util";
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
// with top level await
const data = await readFile("./my/file");
await writeFile(".my/fileCopy", data);
console.log("file copied");
// module.js
let string = "not done";
(async () => {
string = await new Promise(res => {
setTimeout(() => {
res("done");
}, 1000);
});
})();
module.exports = string;
//main.js
const string = require("./module.js")
console.log(string);
setTimeout(() => {
console.log(string)
}, 1500);
CommonJS
// module.js
let string = "not done";
(async () => {
string = await new Promise(res => {
setTimeout(() => {
res("done");
}, 1000);
});
})();
module.exports = string;
//main.js
const string = require("./module.js")
console.log(string); // "not done"
setTimeout(() => {
console.log(string) // "not done"
}, 1500);
CommonJS
// module.mjs
export let string = "not done";
(async () => {
string = await new Promise(res => {
setTimeout(() => {
res("done");
}, 1000);
});
})();
// main.mjs
import { string } from "./module.mjs";
console.log(string);
setTimeout(() => {
console.log(string);
}, 1500);
ES Modules
// module.mjs
export let string = "not done";
(async () => {
string = await new Promise(res => {
setTimeout(() => {
res("done");
}, 1000);
});
})();
// main.mjs
import { string } from "./module.mjs";
console.log(string); // "not done"
setTimeout(() => {
console.log(string); // "done"
}, 1500);
ES Modules
// module.mjs
export let string = "not done";
string = await new Promise(res => {
setTimeout(() => {
res("done");
}, 1000);
});
// main.mjs
import { string } from "./module.mjs";
console.log(string);
setTimeout(() => {
console.log(string);
}, 1500);
ES Modules with TLA
// module.mjs
export let string = "not done";
string = await new Promise(res => {
setTimeout(() => {
res("done");
}, 1000);
});
// main.mjs
import { string } from "./module.mjs";
console.log(string); // "done"
setTimeout(() => {
console.log(string); // "done"
}, 1500);
ES Modules with TLA
// module.mjs
export let string = "not done";
string = await new Promise(res => {
setTimeout(() => {
res("done");
}, 1000);
});
// main.mjs
import { string } from "./module.mjs";
console.log(string); // "done"
setTimeout(() => {
console.log(string); // "done"
}, 1500);
ES Modules with TLA
// module.mjs
export let string = "not done";
string = await new Promise(res => {
setTimeout(() => {
res("done");
}, 1000);
});
// main.mjs
import { string } from "./module.mjs";
console.log(string); // "done"
setTimeout(() => {
console.log(string); // "done"
}, 1500);
ES Modules with TLA
// module.mjs
export let string = "not done";
string = await new Promise(res => {
setTimeout(() => {
res("done");
}, 1000);
});
ES Modules with TLA
ES Modules with TLA
// module.mjs
export let string = "not done";
export const promise = (async () => {
string = await new Promise(res => {
setTimeout(() => {
res("done");
}, 1000);
});
})();
ES Modules with TLA
// main.mjs
import { string } from "./stringModule.mjs";
import { number } from "./numberModule.mjs";
console.log(string);
setTimeout(() => {
console.log(string);
}, 1500);
ES Modules with TLA
// main.mjs
import { promise as p1, string } from "./stringModule.mjs";
import { promise as p2, number } from "./numberModule.mjs";
export const promise = Promise.all([p1, p2]).then(async () => {
console.log(string);
setTimeout(() => {
console.log(string);
}, 1500);
})
const strings = await import(`/i18n/${navigator.language}`);
const connection = await dbConnector();
export let jQuery;
try {
jQuery = await import('https://cdn-a.com/jQuery');
} catch {
jQuery = await import('https://cdn-b.com/jQuery');
}
export let jQuery;
try {
jQuery = export const jQuery = Promise.race([
import('https://fastcdn-a.com/jQuery'),
import('https://fastcdn-b.com/jQuery')
]);
} catch {
jQuery = await import('https://slowcdn.com/jQuery');
}
// main.mjs
import a from "a.mjs"
startApp(a);
// a.mjs
import b from "b.mjs"
const resources = await fetch("/a-resources.json");
export default A(resources, b);
// b.mjs
import c from "c.mjs"
const resources = await fetch("/b-resources.json");
export default B(resources, c);
// c.mjs
const resources = await fetch("/c-resources.json");
export default C(resources);
C
B
A
Main
// main.mjs
import a from "a.mjs"
startApp(a);
// a.mjs
import b from "b.mjs"
import resources from "aResources.mjs"
export default A(resources, b);
// aResources.mjs
export default await fetch("/a-resources.json");
// [...]
C
B
A
Main
No!
CommonJS does not give the possibility to await asynchronous tasks before exporting in any way.
// main.mjs
import a from "a.mjs" // <- declarative
const b = await import("b.mjs") // <- imperative
main();
// a.mjs
const aPromise = await fetch("/a")
export default aPromise;
// b.mjs
const bPromise = await fetch("/b")
export default bPromise;
B
A
Main
B
A
Main
Don't mix imperative and declarative imports