Coder Social home page Coder Social logo

setInterval setImmediate about goja_nodejs HOT 3 CLOSED

dop251 avatar dop251 commented on August 28, 2024
setInterval setImmediate

from goja_nodejs.

Comments (3)

dop251 avatar dop251 commented on August 28, 2024

It's not that simple, if you run this:

setTimeout(() => {
    console.log("timeout");
});

setImmediate(() => {
    console.log("immediate");
});

setTimeout(function() {
    setTimeout(function() {
        console.log('setTimeout')
    }, 0);
    setImmediate(function() {
        console.log('setImmediate')
    });
}, 10);

You'll consistently get

timeout
immediate
setImmediate
setTimeout

Quick research shows that there is no hard requirement on the ordering, for example this article says:

A setTimeout() callback with a 0ms delay is very similar to setImmediate(). The execution order will depend on various factors, but they will be both run in the next iteration of the event loop.

In any case, the node's implementation of event loop is quite complex, and it would be non-trivial to implement something that mimics it precisely. Having said that, I'm always open to consider PRs.

from goja_nodejs.

cjf621 avatar cjf621 commented on August 28, 2024

It's not that simple, if you run this:

setTimeout(() => {
    console.log("timeout");
});

setImmediate(() => {
    console.log("immediate");
});

setTimeout(function() {
    setTimeout(function() {
        console.log('setTimeout')
    }, 0);
    setImmediate(function() {
        console.log('setImmediate')
    });
}, 10);

You'll consistently get

timeout
immediate
setImmediate
setTimeout

Quick research shows that there is no hard requirement on the ordering, for example this article says:

A setTimeout() callback with a 0ms delay is very similar to setImmediate(). The execution order will depend on various factors, but they will be both run in the next iteration of the event loop.

In any case, the node's implementation of event loop is quite complex, and it would be non-trivial to implement something that mimics it precisely. Having said that, I'm always open to consider PRs.

This example it is settimeout block,So it's expected output,but if i have many long time script is run,i can not use setImmediate or other api Insert Before Queue

from goja_nodejs.

dop251 avatar dop251 commented on August 28, 2024

As I said, PRs are welcome.

from goja_nodejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.