1

I am using a printing tool in which i try to find the printers as follows

this.Printers = qz.websocket.connect().then(() => {
               return qzTray.printers.find("found a printer");
            }).then((found) => {
                this.Printers = found;
                return this.Printers;
            }).catch((e) => {
                console.log('failed printing');
                 this.Printers=null;
                 return this.printers
               
}

so when the above runs and it finds a printer this.Printers has a value as. which is correct

this.Printers = "Found a printer"

But when i cant find a printer this.Printers looks like

Promise {<pending>}__proto__: Promise[[PromiseState]]: "pending"[[PromiseResult]]: undefined

so in my catch i tried to assign this.Printers=null as a test to see if it returns that out but it doesnt i still get

Promise {<pending>}__proto__: Promise[[PromiseState]]: "pending"[[PromiseResult]]: undefined

how can i assign this.Printers when it fails to use [[PromiseResult]]: undefined or null?

  • `this.Printers` will initially _always_ contain a pending Promise. It only becomes `"Found a printer"` after the first `.then()` callback is executed/the `.connect()` is resolved. Where/how do you check the value of `this.Printers`? – Ivar Mar 02 '21 at 16:07
  • @Ivar, correct. when its resolved then i get the correct response if their is a printer. i just console.log(this.Printers) to see the result. so if i can somehow assign undefined/null then outside of that function i want to say if (!this.printers){//do something} –  Mar 02 '21 at 16:17
  • 2
    You seem to be misunderstanding how Promises and the asynchronous flow work. Please see [Why is my asynchronous function returning Promise { } instead of a value?](https://stackoverflow.com/questions/38884522/why-is-my-asynchronous-function-returning-promise-pending-instead-of-a-val) It doesn't make much sense to assign a value to `this.Printers` inside of the callback. Just use use `.catch(e => null);` to convert the value to `null` if it fails to find it. (So remove the last `.then()` and `.catch()` from your current code. Then handle the Promise appropriately. – Ivar Mar 02 '21 at 16:31
  • 1
    Your code has a spelling mistake for `this.printers` (capital?). But besides that, if there is an *error* thrown inside the `then` callback, then the `catch` callback will run and set `this.Printers` to `null`. The thing is that you are probably looking at its value at the wrong time. Without further context we cannot say. But it is bad practice anyhow to overwrite a promise object with its resolved value. There is not much more we can say. Give the larger context of what you really need. – trincot Mar 02 '21 at 17:24

1 Answers1

0

Quoting @ivar

You seem to be misunderstanding how Promises and the asynchronous flow work

I agree with this statement, but to take it a bit further, you're doing something very odd, you're redefining a variable inside it's own function.

In some languages, such as Visual Basic, it's normal to use function assignment as a return variable, but this is not normal in JavaScript.

Furthemore, the return keyword in a promise passes the value to the next promise, it does NOT exit a function!

Your code should instead look like this:

var Printers;

qz.websocket.connect().then(() => {
   return qz.printers.find();
}).then((found) => {
   Printers = found;
}).catch((e) => {
   console.log('failed to connect or to find printers');
   Printers=null;       
}).then(() => {
   console.log(Printers);
});

... however this may not be desirable, especially if you're looking to make this function call synchronous. To make it synchronous, try this instead:

async function getPrinters() {
   if(!qz.websocket.isActive()) {
      await qz.websocket.connect();
   }
   return await qz.printers.find();
}

var Printers;
try {
  Printers = await getPrinters();
} catch {
  Printers = null;
}
console.log(Printers);

If you're unsure whether or not to use async and await, see this question: How and when to use ‘async’ and ‘await’

Here's a step-by-step tutorial for using qz-tray and promises: https://www.youtube.com/watch?v=wKIY4gqkIFE

Warning, the async and await keywords are not compatible with Internet Explorer.

tresf
  • 7,103
  • 6
  • 40
  • 101