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

Please add promise example #56

Open
zmorris opened this issue May 23, 2017 · 3 comments
Open

Please add promise example #56

zmorris opened this issue May 23, 2017 · 3 comments

Comments

@zmorris
Copy link

zmorris commented May 23, 2017

Hi, I'm trying to get Synchronize.js working with Inquirer but I've tried everything and can't get it to work with its promises:

package.json

{
  "dependencies": {
    "inquirer": "^3.0.6",
    "synchronize": "^2.0.0"
  }
}

index.js

const inquirer = require('inquirer');
const sync = require('synchronize');

const fiber = sync.fiber;
const await = sync.await;
const defer = sync.defer;

// // works
// inquirer.prompt([{
//     type: 'input',
//     name: 'first_name',
//     message: 'What\'s your first name'
// }]).then(function (answers) {
//   console.log('First name:', answers.first_name);
// });

// doesn't work
fiber(function() {
  const answers = await( 
    inquirer.prompt([{
      type: 'input',
      name: 'first_name',
      message: 'What\'s your first name'
    }]).then(defer())
  );

  console.log(answers);
});
$ yarn
$ node index.js
? What's your first name? Zack

/Users/zackmorris/Desktop/testing/appium-vynyl/node_modules/synchronize/sync.js:111
        fiber.throwInto(err)
              ^
[object Object]

I've tried every permutation, even variations of #41 but can't get it to work.

Can you update your documentation for promises? Or else show how to de-promisify something? I tried wrapping the promise in my own callback function to fit the form of your examples but it still didn't work for some reason.


By comparison, here's the same example with asyncawait and Inquirer:

package.json

{
  "dependencies": {
    "asyncawait": "^1.0.6",
    "inquirer": "^3.0.6"
  }
}

index.js

const async = require('asyncawait/async');
const await = require('asyncawait/await');
const inquirer = require('inquirer');

async (function () {
  const answers = await(
    inquirer.prompt([{
      type: 'input',
      name: 'first_name',
      message: 'What\'s your first name?'
    }])
  );

  console.log('First name:', answers.first_name);
})();
$ yarn
$ node index.js
? What's your first name? Zack
First name: Zack

Thanx!

@zakdav
Copy link

zakdav commented Sep 13, 2017

You can use or give a look to my module node-fibers-synchronize-helper that uses this module synchronize

index.js

const inquirer = require('inquirer');
var synchProm = require('node-fibers-synchronize-helper')

synchProm.executeSynch(function () {

 var answers = synchProm.executePromiseFiberFn(inquirer, inquirer.prompt, [{
     type: 'input',
     name: 'first_name',
     message: 'What\'s your first name'
}])

 console.log(answers);

}, function (err, res) {
if (err)
console.log("err", err)
else
console.log("OK:", res)
})

result:
davide@davide-kubuntu:~/node/test/synchronizeTests$ node .
? What's your first name davide
{ first_name: 'davide' }

You can give a look at my implementation

https://github.com/zakdav/node-fibers-synchronize-helper/blob/master/index.js

In your case i called this methods

var executePromiseFiberIntFn = function (obj, fn, params) {
var defer = sync.defer();
runPromiseFiberFn(obj, fn, params, defer)
var res = sync.await();
return res;
}

var runPromiseFiberFn = function (obj, fn, params, defer) {
fn.apply(obj, params).then(function (res) {
//fn().then(function (res) {
defer(null, res)
}).catch(function (e) {

    defer(e)
})

}

@dsandber
Copy link

dsandber commented Jun 6, 2018

So there isn't any "straight" (without additional code/helpers) way of calling inquirer using synchronize? I tried various permutations of await, defer(), etc, and also could never get it working.

@dreusel
Copy link

dreusel commented Nov 26, 2020

The problem is that then() will pass the result as the first argument, while defer(), generates a classical node-style callback which, like any classical node callback, expects any error as the first argument and the result as the second argument. This is a plain 'how to combine promises with callbacks' type of thing. Instead, try:

fiber(function() {
  // Create the defer first, otherwise await is called before the defer is created.
  const d = defer();
  const answers = await( 
    inquirer.prompt([{
      type: 'input',
      name: 'first_name',
      message: 'What\'s your first name'
    }]).then(r => d(null, r))
  );

  console.log(answers);
});

You could make things look slightly nicer by using nodes util.callbackify, but hopefully my example indicates more clearly what's actually happening.

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

4 participants