So I have this function which I would like to run:
const normal = () => {
return 'working
}
I can log its output and it works fine:
console.log(normal()) // does output: 'working'
Now I would like to run a function delayed
which is exactly the same as normal
only it takes 3 seconds before returning
I would like to be able to log this function call but only after 3 seconds, I am understandably logging undefined
at the moment and have a feeling that it might be possible to do. Anyway I want the following to happen, the function to hang for 3 seconds, then return data:
console.log(delayed()) // should output: 'working'
My attempts:
const toReturn = () => {
return 'working'
}
const runner = (cb) => {
setTimeout(cb, 1000)
}
export const delayed = () => {
return runner(toReturn)
}
console.log(delayed()) // currently outputting: undefined
undefined is because I am not returning anything from runner, if I return setTimout...
in runner, I simply get the whole setTimout function, which is pretty useless.
I know you can simply log after 3 seconds within a callback, but I would like to log what is returned from a setTimeout