There are two files, t1.js and t2.js, which are run using the command line.
$ node t2.js
f2
f1
# I want to output f1 f2, not f2 f1
I tried using async/await
to solve the problem, but I didn't find a way to use it, and runMain()
could not take any parameters.
document content
t1.js:
const f1 = new Promise(async resolve => {
setTimeout(() => resolve('f1'), 1000)
})
new Promise(async () => {
console.log(await f1)
})
t2.js:
new Promise(async () => {
const t1Path = __dirname + '/t1.js'
process.argv = [process.argv[0], t1Path]
require('module').runMain()
// await require('module').runMain() // This is invalid
console.log('f2')
})