nexttick callback and promise callback are not output as expected - node.js

I want to try this code
process.nextTick(() => console.log(8))
Promise.resolve("hi").then(t => console.log(t))
console.log(7);
The expected output is
7
8
hi
but my output is
7
hi
8
I'm familiar with event loop in nodejs, but have no clue why it is not work as expected.
I also use other online compiler, and they all output the expected one.
ex.https://onecompiler.com/nodejs/3yyc9qc65
If someone occur this situation, or understand my environment has something wrong, please let me know.
my Node.js version is 19.6.0.
The environment is code-server 4.9.1 using docker and the server is on unraid 6.11.5.

Related

Error: write EPIPE when running Jest tests on Gitlab CI's personal VPS

So, what I'm trying to do now is simple, create a job at Gitlab CI to run tests that I've made on my own personal VPS. I am using NestJS as my backend. The problem is, for some reason one/some of the test returned a write EPIPE error. Also, the only pattern I get is that the error only occurred for the test that's uploading image using multipart form but not consistently occurred, so like when I ran 3 times, sometimes write EPIPE only occurred 1 time, sometimes twice, sometimes none.
Here is my code snippets for uploading image using multipart form on the test:
it('should be able to upload image', () => {
return request(myHost)
.post('/upload-image')
.expect(200)
.attach('image', './testimage.jpg')
.then((res): any => {
expect(res.body).toEqual({});
});
});
For additional information, testimage.jpg is only 13.9kB, so it's not a big file.
My node version: 14.16.0, Jest version: 26.6.3, NestJS version: 7.6.15,and Ubuntu version: 20.04.
What I've tried is installing libpng-dev package, libfontconfig package, and running tests using -- --silent tag and all of it is not working.
I finally got it working by setting the Connection header to be keep-alive to the tests that has that write EPIPE error. The best possible explanation I can give is when the image is uploading, the connection somehow got closed, so the upload process is stopped. That explains why there is no error when I added console.log(err) to the test and also there's no error on my backend side.
The EPIPE error explanations also mentioned this, which can be seen here:
Source: https://nodejs.org/api/errors.html, on the Common System Errors section.
Some part of the issue that I don't understand is why would the other upload image tests did not encounter this EPIPE error. I had like for sure more than 20 tests that have the upload image part, but only like < 10 encountered it. Besides that, the < 10 tests are randomized too, meaning like not all tests inside this group will 100% encountered it when they were tested.
For example, I have 20 tests for uploading an image, named test1, test2, and so on. For some reason, only some tests encountered the EPIPE error, let's say only test1 to test8 got it (in the real case, the test that got it is random). Then between test1 and test8, sometimes they also got it, sometimes they don't, but the range is always the same (at the first run, the tests who got the EPIPE error was test1, test3, test5, test6. at the second run, the tests who got it could be different from the first one, which is test1, test3, test5, test7, and test8, but never went out of test8).
Perhaps someone can explain this.

socket.io node.js with beaglebone issues

I never normally post stuff here asking why something isn't working, but i've been struggling all day with this issue and hope that someone might be familiar with what's happening and could help.
I took an example from github on how to control GPIOs on the beaglebone black over wi-fi by using socket.io : github.com/lgxlogic/BoneScript-SocketIO
I'm not an expert with javascript but when I follow the exact instructions given and and run the HtmlLedDemo.js file, I get the following error below. My version of node is v.5.9.0 . I have tried updating socket.io and still the same problem. Many thanks in advance.
root#beaglebone:/var/lib/cloud9# node HtmlLedDemo.js
Option log level is not valid. Please refer to the README.
Option browser client minification is not valid. Please refer to the README.
Option browser client etag is not valid. Please refer to the README.
Server running on: http://192.168.0.87:8080
/var/lib/cloud9/node_modules/bonescript/src/my.js:245
callback(resp);
^
TypeError: callback is not a function
at onUnloadSlot (/var/lib/cloud9/node_modules/bonescript/src/my.js:245:13)
at unloadSlot (/var/lib/cloud9/node_modules/bonescript/src/my.js:235:13)
at onWriteSlots (/var/lib/cloud9/node_modules/bonescript/src/my.js:210:43)
at onReadSlots (/var/lib/cloud9/node_modules/bonescript/src/my.js:199:13)
at onFindCapeMgr (/var/lib/cloud9/node_modules/bonescript/src/my.js:174:9)
at Object.exports.load_dt (/var/lib/cloud9/node_modules/bonescript/src/my.js:157:5)
at onDTBOExists (/var/lib/cloud9/node_modules/bonescript/src/my.js:332:26)
at onDTBOExistsTest (/var/lib/cloud9/node_modules/bonescript/src/my.js:279:13)
at Object.exports.create_dt (/var/lib/cloud9/node_modules/bonescript/src/my.js:274:9)
at Object.exports.setPinMode (/var/lib/cloud9/node_modules/bonescript/src/hw_capemgr.js:102:12)
root#beaglebone:/var/lib/cloud9#
Your error message says "callback is not a function". If you have a look at the supplied call stack, you can see that on line 245 of your "my.js" file, you attempt to call a function by the variable name callback. What this error message tells you is that whatever this variable contains, it is not a function.

Node - console.log + return values

I'm just getting started in programming and am using VSC and installed Node and am using it to run my files. My console.logs works, but I can't get return values when I invoke functions. What am I doing wrong?
Node.js is asynchronous. 99% of all functions you write are going to be "non-blocking". If you don't quite understand what that is, I highly suggest you google up on the node.js event loop, and what it means to be "asynchronous".
Once you figure that out, start using the async/await syntax, or "promise" syntax.
Edit:
I see you posted more information. Based on what you're doing, it actually has nothing to do with being asynchronous.
The problem is that you're just returning a string value of hihihihihi... and that's it. You don't print it out anywhere. You need to wrap your call to function a() inside console.log(). So like: console.log(a());

is it possible to "yield" a async process in node v10.x?

i'm using the request module to fetch image from website and pipe it into local file, the code is like:
url = http:/xxx.com/x.jpg;
if(url){
request(url).pipe(localFilePath);
}
if(xxx){
// save the localFilePath to db;
redirect('/index');
}
// The question is the filePath is needed in the index page, so if the file has not downloaded yet, then it can not show the file on index page.
i tried.
request(url).pipe(...).on('end',function(){
....
});
but it seems does't work..
so, i wonder how to do like :
yield xxxxx in node v0.11.x to pause the process until the file is already downloaded completely?
thanks
Yield is only presently available in Node 0.11 (when using the –harmony flag), but this is an unstable release that is probably not suitable for any kind of production use. Node 0.12 shouldn’t be too far away though, as 0.11 has been in development for a while now. So the good news is generators will be available in a stable Node.js release near you very soon!
If you want to stick with 0.10.x you will have to use callbacks or promises for now.

Cordova CLI - passing args when called from node app

I am trying to use the cordova CLI in a larger Node.js application and running into trouble when trying to figure out how to pass the required arguments to some of the functions.
Specifically, I need to pass the "release" flag to the build function. I've tried a lot of different combinations, but I've yet to get success
using cordova = require('cordova');
I've tried:
cordova.build('blackberry10', '--release', function(){ //callback code });
and I've tried
cordova.build('blackberry10 --release', function(){//callback code});
and every other combination I can think of. In the first example, the blackberry10 argument get's processed and the call back does, but the release argument does not.
Any help or thoughts are appreciated.
Answering my own question here, the right answer was to pass the arguments as an object.
cordova.build({platforms: ['blackberry10'], options: ['--release', '--keystorepass', 'mysecret']}, function(){
//callback code
});

Resources