Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript execution with inconsistent order

I am experiencing some issues on TypeScript 2.4.1, Node.js v8.1.3, and VS Code 1.14.0.

For some reason, my experimental code does not print in order. Especially rest and foreach section.

Here is my test page.

Am I missing something here?

GggDataStructuresIntTests.ts

var dotdotdotFun = function () {
    let [first, ...rest] = [1, 2, 3, 4, 5];
    console.log("first", first); // outputs 1
    console.log("rest", rest); // outputs [ 2, 3, 4 ]
    rest.forEach((item) => {
        console.log("forEach", item);
    });
}
dotdotdotFun();

tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "outDir": "js/",
        "sourceMap": true,
        "lib": [ "es2015" ],        
        "watch": true
    },
    "exclude": [
        "node_modules",
        "typings/browser.d.ts",
        "typings/browser"
    ]
}

launch.json

{

    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Node.js",            
            "program": "${file}",
            "outFiles": [
                "${workspaceRoot}/js/**/*.js"
            ]
        }        
    ]
}

There is some outputs;

Output 1:

enter image description here

Output 2:

enter image description here

Output 3:

enter image description here

like image 776
Teoman shipahi Avatar asked Feb 04 '26 03:02

Teoman shipahi


1 Answers

After Yeshan Jay's comment, I did more research on console.log() over Node.js v8.2.0 Documentation.

There is a statement like below;

Console

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

So seems like it is not fault of TypeScript compiler at all. (I checked also compiled JS, and everything was fine). Since I was running compiled js via Node.js engine, issue was starting from that moment. That's why issue cannot be reproducible at browser level.

I did not check with prev Node engines, but on this new version (>= 8) seems like console.log() should not be assumed as synchronous at all.

like image 130
Teoman shipahi Avatar answered Feb 06 '26 17:02

Teoman shipahi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!