FAQ: JavaScript Promises - The Node setTimeout() Function

Is this function invoked as told in the exercise?

As far as I understand the meaning of invoking a function the function returnPromiseFunction() is not invoked in the following code:

const returnPromiseFunction = () => {
return new Promise((resolve, reject) => {
setTimeout(( ) => {resolve(‘I resolved!’)}, 1000);
});
};

const prom = returnPromiseFunction();

Hi! could you figured it out? I have the very same problem! :frowning_face:

1 Like

^this
i ask myself too

i can resume that i never saw it before - different tutorials, different time
But maybe i’m wrong, i dont know anymore

it is mentioned in a lesson here “Moving forward, we’ll be simulating this by providing you with functions that return promises which settle after some time.”
On the lesson:
The Node setTimeout() Function

But in the review of this chapter it is stated as
" setTimeout() is a Node function which delays the execution of a callback function using the event-loop."

more questions then answers …

  1. They will not produce the same output

  2. Just because the same output is achieved does not mean two processes are the same

setTimeout should be invoked outside of your callback function usingSTO

can someone enlighten us. what is the Error message for this? should we complete and revert the app.js? coz in the previous exercise some of us didn’t get the lib it should show but doesn’t. is this the effect of the previous activities not being correct but the checker is interpreting it as correct even though the other file is not created? Thanks! will help us alot!

I am also getting the error in the console and am unsure what I am doing wrong…

$ node app.js
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
path.js:1142
cwd = process.cwd();
^

Error: ENOENT: no such file or directory, uv_cwd
at Object.resolve (path.js:1142:25)
at startup (bootstrap_node.js:129:32)
at bootstrap_node.js:542:3
$

but resolve is itself a function,isnt it?

why prom have status of pending?

setTimeout() and setInterval() functions allow you to execute a piece of JavaScript code/function at a certain point in the future. setInterval repeats the call, setTimeout only runs it once.

setTimeout(expression, timeout); runs the code/function once after the timeout. It is a time based code execution method that will execute script only one time when the interval is reached, and not repeat

again unless you gear it to loop the script by nesting the setTimeout object inside of the function it calls to run. If geared to loop, it will keep firing at the interval unless you call clearTimeout(). If you want

something to happen one time after some seconds Then use setTimeout… because it only executes one time when the interval is reached.

setTimeout(function() {
  console.log('Wait 3 seconds and I appear just once');
}, 3000);

setInterval(expression, timeout); runs the code/function repeatedly, with the length of the timeout between each repeat. It is a time interval based code execution method that has the native ability to repeatedly

run specified script when the interval is reached. It should not be nested into its callback function by the script author to make it loop, since it loops by default. It will keep firing at the interval unless you call

clearInterval(). If you want to loop code for animations or clocks Then use setInterval.

setInterval(function() {
  console.log('Every 3 seconds I appear on your console');
}, 3000)

Why use the setTimeout function

to create a delay of a fraction of a second or a few seconds before stuff continues.
and to demonstrate asynchronous stuff.

Why does the exercise (and the previous, return an error in the bash console?

$ node app.js
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
path.js:1142
          cwd = process.cwd();
                        ^

Error: ENOENT: no such file or directory, uv_cwd
    at Object.resolve (path.js:1142:25)
    at startup (bootstrap_node.js:129:32)
    at bootstrap_node.js:542:3

Is it a mistake on my end, a bug, or purposeful?

just some feedback for codecademy: the use of line in this exercise is sometimes interchangeable with queue which caused me some slight confusion… a line of code put in line with other lines of code… the use of queue would never create this tautology. moreover the use of line in this way is glaringly colloquial and inconsistent with previous copy in this lesson.

I was also having that issue.
I found that once I refreshed the page, the problem resolves and node prints app.js in the terminal.
Maybe it’s that the newly typed code is not saved until the page is refreshed, though don’t know if that’s what it is.
Hope this is helpful.

I can't print my working directory using the terminal on codecademy please , an assistance will be greatly appreciated.