0

So I am really lost on this one. I use ubuntu, and nvm for node. I even removed the version of node that was installed with apt to make sure

node --version
> v10.10.0

npm --version
> 6.4.1

So I create-react-app and use this simple code

import React from 'react'
import ReactDOM from 'react-dom'

class App extends React.Component
{
    componentDidMount()
    {
        const a = setTimeout(() => {return}, 5000)
        console.log("a = ", a)
    }

    render()
    {
        return (
        <div>
            hello
        </div>)
    }
}

ReactDOM.render(<App />, document.getElementById('root'))

Then I npm start . When accessing the page, the console displays a number (a = 4). However https://nodejs.org/api/timers.html states that setTimeout should return a Timeout object, not an id.

This is a problem to me because I wish to call refresh on the stored variable, and cannot right now (I have found no ways of retrieving the timeout object based on the id).
Is it a react problem that is not using the correct node version? Or is it a regression? I have tried the 10.2 version as well without luck.

Thanks in advance!

  • Are you rendering this React code server side and getting the log in your node console, or is this log showing up in the browser console? – caffeinated.tech Sep 10 '18 at 09:12
  • The log is user side, I am running ``npm start`` server-side. In any case ``a.refresh()`` or ``a.ref()`` crashes with ``TypeError: a.ref/a.refresh is not a function`` so it is not a simple displaying issue, as far as I understand – snow_lemurian_snow Sep 10 '18 at 09:15

2 Answers2

1

Your react code is being executed in a browser, not in NodeJS. This means the documentation you need to review for setTimeout is different. See MDN's documentation on setTimeout showing that it should return a timeoutID which can be passed to clearTimeout.

As far as I know, there is no equivalent for timer.refresh() in the browser, but you can probably find a library which implements this behaviour, or implement it yourself.

caffeinated.tech
  • 6,428
  • 1
  • 21
  • 40
0

"Timeout objects" are in fact just the id of the timeout which is a number. So I think the API behaves as specified.

Maybe the confusion is that in Nodejs it returns an Object and in the Browser it returns an id. Your code gets executed in the browser and not in Nodejs. See discussion herr: What does setTimeout return?

Herku
  • 7,198
  • 27
  • 36
  • Then I am not understanding correctly. https://nodejs.org/api/timers.html#timers_class_timeout This says it gets returned by ``setTimeout`` , and as I said, I wish to call functions on it, but both ``a.ref()`` and ``a.refresh()`` tells me ``TypeError: a.ref/a.refresh is not a function``, as ``typeof a`` is a Number. How am I supposed to call on those functions? I did read the link you mentionned before posting, and https://stackoverflow.com/a/19375604/7676564 comforted me in the idea something was not behaving properly – snow_lemurian_snow Sep 10 '18 at 09:05
  • https://github.com/nodejs/node-v0.x-archive/issues/5728#issuecomment-19817034 this also says that it's been supposed to be this way in the inception of it, although a breaking change may have happen – snow_lemurian_snow Sep 10 '18 at 09:09
  • I guess the other question was better at explaining the confusion that was going on about what Javascript environment executes your code. – Herku Sep 10 '18 at 19:50