Let's say we want to call a function that does some work, then we want to do our own work, then let the function know that we've completed our work. We could achieve that by doing this:
var s = () => {
console.log('start');
return Promise.resolve(() => { console.log('run after finish') });
};
const start = s();
console.log('then do work');
start.then(f => f());
This will print in the following order:
start
then do work
run after finish
Obviously you don't have to use promises for this though. In fact, I wouldn't recommend it. Just return the object you want to run.
var s = () => {
console.log('start');
return { finish: () => { console.log('run after finish') } };
};
const work = s();
console.log('then do work');
work.finish();