Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

await does not return until the terminal is moved #3

Closed
sivabudh opened this issue Sep 20, 2017 · 3 comments
Closed

await does not return until the terminal is moved #3

sivabudh opened this issue Sep 20, 2017 · 3 comments

Comments

@sivabudh
Copy link

sivabudh commented Sep 20, 2017

@yushulx I re-wrote the code like this:

main.js

// Express
const express = require('express');
const app = express();

// Lodash
const _ = require('lodash');

// DBR
const dbr = require('./build/Release/dbr');
dbr.initLicense("t0068MgAAAGvV3VqfqOzkuVGi7x/PFfZUQoUyJOakuduaSEoI2Pc8+kMwjrojxQgE5aJphmhagRmq/S9lppTkM4w3qCQezxk=");

// Promisify
const {promisify} = require('util');
const decodeFilePromise = promisify(dbr.decodeFileAsync);

app.get('/', async (req, res) => {
    console.log("Received a barcode scan request!");
    try {
        const oneDimensionType = 0x3FF;
        const scannedResults = await decodeFilePromise('test.jpg', oneDimensionType);

        const imeiResults = _.uniq(_.map(_.filter(scannedResults, ['format', 'CODE_128']), 'value'));
        console.log(`Successfully scanned the image: ${imeiResults}`);

        res.send(`IMEI results: ${imeiResults}`);
    }
    catch (err) {
        console.log(`Failed to scan the image: ${err}`);
        res.send('Could not scan the barcode!');
    }
});

app.listen(3000, () => {
    console.log('Example app listening on port 3000!');
});

Reference (forked from your repo):
https://github.com/sivabudh/linux-barcode-sdk-node-wrapper/blob/dbr-async-await/main.js

From the outset, it should have worked fine, right? But what I found was that await never returned until I moved the Terminal screen (!!). If this is unclear, please kindly see the GIF below.

GIF demonstrating the problem:
https://cl.ly/mcb6

@sivabudh
Copy link
Author

sivabudh commented Sep 20, 2017

@yushulx

I posted the same question on StackOverflow, and got an interesting comment.

Resizing a terminal sends particular signals to a process. Perhaps the library you're using relies on those, for some reason. However, it's hard to say without the library code; is that available from somewhere?

Ref: https://stackoverflow.com/questions/46321805/strange-node-await-never-unblocks?noredirect=1#comment79606664_46321805

@yushulx
Copy link
Contributor

yushulx commented Sep 28, 2017

@sivabudh it seems to be a known Express issue. tjanczuk/edge#325. I can do nothing with my code. I've tested your code and found when the libuv work's done, it blocked. Once you do something, such as refresh your page or send interruption signal in the terminal, the code will continue to run.

I suggest that you can use the traditional way as follows:

// Express
const express = require('express');
const app = express();

// Lodash
const _ = require('lodash');

// DBR
const dbr = require('./build/Release/dbr');
dbr.initLicense(
    "t0068MgAAAGvV3VqfqOzkuVGi7x/PFfZUQoUyJOakuduaSEoI2Pc8+kMwjrojxQgE5aJphmhagRmq/S9lppTkM4w3qCQezxk=");

// Promisify
const {promisify} = require('util');
const decodeFilePromise = promisify(dbr.decodeFileAsync);

app.get('/', (req, res) => {
  console.log("Received a barcode scan request!");
  try {
    const oneDimensionType = 0x3FF;
    dbr.decodeFileAsync('test.jpg', 0x3FF, (err, scannedResults) => {
        console.log(scannedResults);

      const imeiResults = _.uniq(
          _.map(_.filter(scannedResults, ['format', 'CODE_128']), 'value'));
      console.log(`Successfully scanned the image: ${imeiResults}`);
      res.send(`IMEI results: ${imeiResults}`);
    });

  } catch (err) {
    console.log(`Failed to scan the image: ${err}`);
    res.send('Could not scan the barcode!');
  }
});

app.listen(3000, () => { console.log('Example app listening on port 3000!'); });

@sivabudh
Copy link
Author

@yushulx If that's the case, I'm thinking about dropping Express, and use some other framework. I probably will use a similar bare bone framework. I will update you.

@yushulx yushulx closed this as completed Aug 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants