Search code examples
javascriptnode.jspromisequci

Resolve promise using promise implementation of node package


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);
  }

Solution

  • 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".