-1

I am using the uci node package which uses the Q library for promises in their source and thus makes the following promises possible, but I can't resolve the promise and propagate outputPromise due to deferred.resolve() not being defined. How would I resolve the following promise and propagate outputPromise?

var Stockfish = require('uci');
var stockfish = new Stockfish('..... /stockfish-6-64');

class Engine { ...

checkForBetterMoves(board, callback) {
    var moves = {};
    console.log('hello');
    var outputPromise = stockfish.runProcess().then(function() {
      console.log('Started');
      return stockfish.uciCommand();
    }).then(function(idAndOptions) {
      console.log('Engine name - ' + idAndOptions.id.name);
      return stockfish.isReadyCommand();
    }).then(function() {
      console.log('Ready');
      deferred.resolve("Test"); //Error deferred not defined
    });
    console.log(outputPromise);
  }
Henry Boldizsar
  • 489
  • 1
  • 8
  • 25

1 Answers1

1

You should simply be able to return a raw value from a .then handler which Q will wrap in a promise that immediately resolves, and return it.

var outputPromise = stockfish.runProcess().then(function() {
  console.log('Started');
  return stockfish.uciCommand();
}).then(function(idAndOptions) {
  console.log('Engine name - ' + idAndOptions.id.name);
  return stockfish.isReadyCommand();
}).then(function() {
  console.log('Ready');
  return "Test";
});

The outputPromise above will ultimately resolve with the value "Test".

CatDadCode
  • 58,507
  • 61
  • 212
  • 318
  • When I console.log(outputPromise); after that, it ends up logging it before the promise is run and is undefined. Any ideas? – Henry Boldizsar Jul 07 '15 at 00:31
  • That's because your promise hasn't resolved yet. You'd need to do `outputPromise.then(function (result) { console.log(result); });` then you'd get `"Test"` logged to the console. You'll never be able to get the resolved promise value back out to the same scope as the `outputPromise` definition, if that's what you're trying to do. That code is long over with by the time the promises resolve and the resolution value will never be able to exist there. – CatDadCode Jul 07 '15 at 00:32
  • Then how would I access outputPromise when calling checkForBetterMoves? – Henry Boldizsar Jul 07 '15 at 01:01
  • You would return the promise from `checkForBetterMoves` and handle the `.then` callback wherever you need that value. – CatDadCode Jul 07 '15 at 01:01
  • When I run `new Engine(board).then(function (result){ console.log("result ", result); });`, I get the error `TypeError: Object [object Object] has no method 'then'` – Henry Boldizsar Jul 07 '15 at 02:43
  • 1
    Did you mean `(new Engine(board)).checkForBetterMoves().then(...)`? – CatDadCode Jul 07 '15 at 02:45
  • No, but when I run `(new Engine(board)).checkForBetterMoves().then(...)` I get `TypeError: Object [object Object] has no method 'then'` – Henry Boldizsar Jul 07 '15 at 02:56
  • Are you returning `outputPromise` from `checkForBetterMoves`? You aren't in your example code. Replace that console.log at the end with a `return outputPromise;`. – CatDadCode Jul 07 '15 at 02:57
  • I was returning everything after `stockfish.runProcess().then(function() {` but after now after returning `outputPromise`, it never gets to the `.then()` here `(new Engine(board)).checkForBetterMoves().then(function (result){...`. Although the script ends up running without error messages. – Henry Boldizsar Jul 07 '15 at 03:08