Unhandled Promise Rejection

Supporting Browser-based Javascript and Node.js
AdamLee
Phidgetly
Posts: 34
Joined: Wed Jul 17, 2019 9:28 am
Contact:

Unhandled Promise Rejection

Post by AdamLee »

I updated our Phidget22 package the other day and now I'm intermittently noticing these:

Code: Select all

(node:15) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2178)
(node:15) UnhandledPromiseRejectionWarning: Error
	at new Pt (/usr/src/api/node_modules/phidget22/phidget22.min.js:1:13902)
	at Object.onReply (/usr/src/api/node_modules/phidget22/phidget22.min.js:2:17461)
	at Bt.N.Bt.ondatamessage (/usr/src/api/node_modules/phidget22/phidget22.min.js:2:19938)
	at Bt.onmessage (/usr/src/api/node_modules/phidget22/phidget22.min.js:2:15368)
	at Bt.Rt (/usr/src/api/node_modules/phidget22/phidget22.min.js:2:12155)
	at Socket.emit (events.js:198:13)
	at addChunk (_stream_readable.js:288:12)
	at readableAddChunk (_stream_readable.js:269:11)
	at Socket.Readable.push (_stream_readable.js:224:10)
	at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
Anyone else? I'm having a really hard time tracking down where this is happening since it's asynchronous.
Adam Lee
Software Engineer
www.activatedresearch.com
arpadsooky
Fresh meat
Posts: 1
Joined: Wed Sep 02, 2020 7:44 am
Contact:

Re: Unhandled Promise Rejection

Post by arpadsooky »

Hi,
I have the same error with the example code. Is there any solution?

I use
- node version 11.6.0
- npm version 6.5.1
- phidget22 version 2.6.9

thanks
ruisebastiao
Fresh meat
Posts: 1
Joined: Tue Oct 06, 2020 6:58 am
Contact:

Re: Unhandled Promise Rejection

Post by ruisebastiao »

you have to handle/catch the promise errors:

Code: Select all

conn.connect().then(runExample).catch((error) => {
	console.error("err:"+error)
});
superninja
Phidgetsian
Posts: 8
Joined: Tue Aug 10, 2021 4:00 pm
Contact:

Re: Unhandled Promise Rejection

Post by superninja »

Hi, I am getting this error on some API calls, for example the DigitalInput getState() function. In fact, the sample code for this function, in the API documentation, also gives this error.
Is this some error in the libs? Or in the sample code (and mine)?
Thanks.
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Unhandled Promise Rejection

Post by mparadis »

You can try putting a catch block on the line with getState and the print the error just like the example does with connect and open. The error message you get will be less cryptic than Javascript's promise rejection.
superninja
Phidgetsian
Posts: 8
Joined: Tue Aug 10, 2021 4:00 pm
Contact:

Re: Unhandled Promise Rejection

Post by superninja »

I'm getting an error such as:
name: 'PhidgetError',
errorCode: 51,
message: 'Unknown or Invalid Value'


Maybe the issue is in what I'm trying to do - I'm using a Beam break sensor, and after connecting to the hub and opening the digital input, I want to know the value of the sensor before running other code (which depends on the state of the sensor).

If I use the onStateChange handler, I can see the changes and it works fine, but is it possible to read the initial value, straight after opening the digital channel? Meaning when I start the application, if there is no change in the state of the sensor, can I know what the current state is?

Something like:

Code: Select all

        digitalInput0.open(5000).then(async function() {
            try {
                currentState = digitalInput0.getState();
            } catch (error) {
                console.log("CATCH ERROR", error) // this is printing the error 51
            }
        });
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Unhandled Promise Rejection

Post by mparadis »

You get the unknown value exception when you try to get the value for something that doesn't exist (yet). In this case, because you're trying to get State before the channel fully initializes.

When you use the onStateChange handler, it will give you an initial event with the current state of the input as soon as it's available (regardless of whether it's 1 or 0), so using the event will accomplish what you were trying to do.
superninja
Phidgetsian
Posts: 8
Joined: Tue Aug 10, 2021 4:00 pm
Contact:

Re: Unhandled Promise Rejection

Post by superninja »

The onStateChange handler is never called until I interact with the Beam break sensor. So, if there is an object in the sensor when I start the application, meaning the state should be reported as 'false', this is never the case. Nothing is reported until the state changes. I checked, and indeed once there has been a change in the state (i move the object), the getState is working correctly.

This is a problem for the application I'm trying to develop, as I need to detect (at startup) whether there is an object or not in the sensor. As I explained, there seems to be no way to obtain this information, until it actually changes.

Do you know if there is any solution? Or is that just the way the sensor works? Thanks a lot for your help so far.
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Unhandled Promise Rejection

Post by mparadis »

I just tested the PRX2300 in Javascript using our sample code and it successfully fires an event whether the sensor starts in a true or false state. Here's the code I used:

Code: Select all

var phidget22 = require('phidget22');

function runExample() {
	var digitalInput0 = new phidget22.DigitalInput();

	digitalInput0.setIsHubPortDevice(true);
	digitalInput0.setHubPort(0);

	digitalInput0.onStateChange = function onDigitalInput0_StateChange(state) {
		console.log('State: ' + state.toString())
	};

	digitalInput0.open(5000).then(function() {

		setTimeout(function () {
			digitalInput0.close();
			process.exit(0);
		}, 5000);
	});
}

var conn = new phidget22.Connection(5661, 'localhost');
conn.connect().then(runExample);
You should also double check your Phidget22 libraries and firmware version on your HUB0000 just in case this is a behaviour that has been patched.
superninja
Phidgetsian
Posts: 8
Joined: Tue Aug 10, 2021 4:00 pm
Contact:

Re: Unhandled Promise Rejection

Post by superninja »

I'm using a wireless VINT hub (HUB5000_0), sorry I didn't mention. I upgraded the firmware to version 1.0.53 and ran your code. I only get a state update when I put my finger in the sensor, no initial state update event is sent :(
If this is working as expected with the HUB0000, thats great because this project will be implemented with this hub. But right now I only have a wireless hub for development. Can you check on the HUB5000? Maybe this was patched for the HUB0000 only.

Thanks!!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest