Reputation: 23052
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:
Output 2:
Output 3:
Upvotes: 3
Views: 965
Reputation: 23052
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.
Upvotes: 1