Let's start like this:
'use strict'
const timeoutPromise = (time) => {
return new Promise((resolve, reject) => { setTimeout(() =>
{ console.log('howdy'); resolve('done') }, time) })
}
below we do...
Ending A) - simple promise
console.log('start')
timeoutPromise(1000)
console.log('end')
start and end will appear immediately.
it will take another second to see 'howdy' and get our terminal prompt back. (so in that sense the main script is kept alive, but presumably not what the OP wants...)
Ending B) - waiting for promise return
console.log('start')
return timeoutPromise(1000)
console.log('end')
start will appear, after 1 second 'howdy' will appear. 'end' is unreachable. So here we truly wait for the promises and could do things with them…
Ending C) - then()
console.log('start')
return timeoutPromise(1000).then((result) => {
console.log('end', result)
process.exit(123) // usually 0 for 'ok', just demo!
}
)
start will appear, one seconds goes by and 'howdy', 'end' and 'done' appears. And could be used to send a return value.
$>node haveMainWait.js
start
howdy
end done
$>echo $? // return value of last command
123
Almost certainly you want a .catch()
after the .then()
in case the promise gets rejected... (and returning a nonzero exit code only in that case)
Instead of a single Promise like timeoutPromises(…)
you could of course use Promise.all(…)
or an async/await-Function (which has to be wrapped back to promises somewhere on the hierarchical way up... you have that covered here, too).