FAQ: JavaScript Promises - The Node setTimeout() Function

This community-built FAQ covers the “The Node setTimeout() Function” exercise from the lesson “JavaScript Promises”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Asynchronous JavaScript
Asynchronous JavaScript

FAQs on the exercise The Node setTimeout() Function

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Does the setTimeout() function really have to invoke function first and only then the number setTimeout(ExampleFunction, 1234) ? Or can it invoke number first setTimeout(1234, ExampleFunction) ? Why?

1 Like

from what I have read, it does seem that way, as the documentation formats the method in such way.

https://www.w3schools.com/jsref/met_win_settimeout.asp

but try it anyway. always question and test hypothesis on your own. we need to become better scientists :slight_smile: . good question though!

What is the difference between
setTimeout(resolve('I resolved!'), 1000);
and
setTimeout(( ) => {resolve('I resolved!')}, 1000);
?

6 Likes

In the first snippet there’s no function, and the setTimeout needs a function for it’s first argument :slight_smile:

2 Likes

what happens if in the callback function we only invoke resolve() and not invoke reject, like in this case: `const returnPromiseFunction = () => {
return new Promise((resolve, reject) => {
setTimeout(( ) => {resolve(‘I resolved!’)}, 1000);
});
};

const prom = returnPromiseFunction();`

…and then promise will be rejected???

It seems like in this example, there will be a definite resolve call as the exercise is made that way.

If you remember in the previous exercise, we created a reject when we had a condition that expects failure in some way.

I guess this exercise is just to display that how async operations happen. The async statements continue to execute and simultaneously with the sync ones and we observe the in results.

For some fun you can play with the code by setting timeout for the first and last statements as well, and then share your experience with others.

both are same they will produce the same output

if promise itself runs synchronously then how can it handle asynchronous actions?

const delayedHello = () => {
console.log(‘Hi! This is an asynchronous greeting!’);
};

setTimeout(delayedHello, 2000);

What is the time parameter in setTimeout() for? If we setTimeout(delayedHello, 0) , delayedHello will still be added to macrotask queue and even-loop will make sure any synchronous code from the program will run, any code in front of delayedHello will run, things will still work asynchronously right? If this is the case, why do we set time for setTimeout() ? Is it even necessary?

Hi Mentors,

Regarding Step 3 which says

  1. Take a moment to predict the output of this program.
    Whenever you’re ready, type node app.js in the terminal and hit enter.
    See if the program’s output in the terminal lines up with what you expected.
console.log("This is the first line of code in app.js.");
// Keep the line above as the first line of code
// Write your code here:
const usingSTO =() =>{
  console.log("This is the first line of synchronous code.");
  setTimeout(usingSTO, 3000);
};

// Keep the line below as the last line of code:
console.log("This is the last line of code in app.js.");

I was expecting output as. : console.log("This is the first line of synchronous code.");

However in bash I see :

$ node app.js
This is the first line of code in app.js.
This is the last line of code in app.js.
$ 

Could you please explain :slight_smile:

how come i recieved all the green check marks showing that my code was correct but my string did not print to the console?

Hi,
you didn’t call your ‘usingSTO’ function.

Hi!

is the setTimeout function only used to imitate a function that takes some time to explain this proces for us or is the function also used in programs to better time operations? could someone give a usecase?

Hello everyone,
In setTimeout (browser API) the first argument must be a function and we should pass it by reference(we can’t invoke it), or we can pass it to another callback function as in your second example (setTimeout(( ) => {resolve('I resolved!')}, 1000);).
Because we should pass the firest argument as a reference(not invoke it) setTimeout(resolve("I resolved!"), 1000); will throw an error.
Hope this helps and you can read more about them here: MDN setTimeout

Thank you @marcusde36ste
Another question. How should I modify the following code to invoke ‘usingSTO’ function exactly 3 times?

console.log("This is the first line of code in app.js.");
// Keep the line above as the first line of code
// Write your code here:
const usingSTO = () => {
console.log('This is a line of synchronous code.');
setTimeout(usingSTO, 1000);

};
usingSTO();


// Keep the line below as the last line of code:
console.log("This is the last line of code in app.js.");

Thanks =)

by calling usingSTO(); three times. does that answer your question?

@marcusde36ste
Apologies. What i meant was printing the output line ‘This is a line of synchronous code.’ exactly 3 times:

My earlier code causes the output line ‘This is a line of synchronous code.’ to print indefinitely.

Oh yes, you’re right.
You are calling usingSTO inside the usingSTO function. so every call of usingSTO will call a new usingSTO, indefinetly… :smiley:

1 Like

Im getting this error is there something that I need to download first?

1 Like