I am trying to simply chain some async functions together so they operate in order. I am probably making a basic error.
I just want the printFirst to run and then once complete for printSecond to run.
below is my code that does not work
const printFirst = ()=>{
console.log('First')
}
const printSecond = ()=>{
console.log('Second')
}
// Print this first (after 2 sec)
const first = () =>{
setTimeout(printFirst,2000)
}
// Once the above function is run then run this
const second = () =>{
setTimeout(printSecond,2000)
}
first()
.then(second())
I get the error
node timeout.js
file:///Users/bob/Documents/dev/javascript/node/nodeScratch/timeout.js:20
.then(second())
^
TypeError: Cannot read properties of undefined (reading 'then')
at file:///Users/bob/Documents/dev/javascript/node/nodeScratch/timeout.js:20:1
at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)
I did see a post here How do I access previous promise results in a .then() chain? which suggests that I may not have to use "then". HOwever I am not sure if that is actually relavant to my usecase.