Search code examples
javascriptes6-promise

what is the third param in setTimeout function?


I was reading some code using the Promise object.

There is a setTimeout function with three parameters, I am wondering what does the third parameter do? Because usually it only has two parameters.

The code is like below:

function timeout(ms) {
    return new Promise((resolve, reject) => {
        setTimeout(resolve, ms , 'done');
    });
}
timeout(1000).then(value => {
    console.log(value);
});

I noticed that the third parameter is passed to the resolve function, but why can I use it in the function in then? How does it work?


Solution

  • It's for passing additional parameters (arguments) that you might use in your function. These are not obligated.

    Here's a link with more information about parameters and arguments: https://www.w3schools.com/js/js_function_parameters.asp

    Hope this helps.