简体   繁体   中英

Javascript promise never resolves

I am using rotateByDegrees from a library called node-poweredup in a typescript project:

motor.rotateByDegrees(20,10).then(function(value) {console.log("test");}, function(value) {console.log("error");});

I would expect to see "test" after successful completion, but the promise never resolves. If I use await, it hangs on the await line forever. Replicating the syntax that appears to be used in the rotateByDegrees function:

let promise = new Promise((resolve) => { return resolve(); });

does not compile, I get error TS2794: Expected 1 arguments, but got 0. Did you forget to include 'void' in your type argument to 'Promise'? I can make it compile and behave as expected with resolve(true) , but how does it compile in the library then? Do I misunderstand promises? Some feature in typescript? A bug in the library? I am a newbie to JavaScript, I don't want to over-complicate this question by including irrelevant details. If you can give me hints on what I am missing and how to debug this, I can provide all relevant details.

Thanks to the helpful comments I was able to narrow it down to the compilation of the library. I did in fact not use a pre-compiled binary but had to compile the library myself using electron-rebuild to make the bluetooth adapter work. I did the following test:

git clone https://github.com/nathankellenicki/node-poweredup.git
cd node-poweredup
npm install
npm run build

this compiles without error. I created the following test file

const PoweredUP = require("node-poweredup");

const poweredUP = new PoweredUP.PoweredUP();
poweredUP.scan(); // Start scanning for hubs

console.log("Looking for Hubs...");

poweredUP.on("discover", async (hub) => { // Wait to discover hubs

    await hub.connect(); // Connect to hub
    console.log(`Connected to ${hub.name}!`);

    const motorA = await hub.waitForDeviceAtPort("A"); // Make sure a motor is plugged into port A
    motorA.rotateByDegrees(20,10).then(function(value) {console.log("test");});

});

and get the expected output:

node-poweredup$ node test.js 
Looking for Hubs...
Connected to MyHub2!
test
Connected to MyHub3!
test

When I changed the first line to

const PoweredUP = require(".");

to make it use my self-compiled binary I get

node-poweredup$ node test.js 
Looking for Hubs...
Connected to MyHub2!
Connected to MyHub3!

Of course this is only a partial answer because I still don't know why it compiles differently on my machine, but at least I have an idea where to start searching for the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM