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

Using async with Promises #1086

Closed
RayKahn opened this issue Mar 26, 2016 · 7 comments
Closed

Using async with Promises #1086

RayKahn opened this issue Mar 26, 2016 · 7 comments
Labels

Comments

@RayKahn
Copy link

RayKahn commented Mar 26, 2016

Any idea if this is possible or on the way?

Thanks in advance.

@aearly
Copy link
Collaborator

aearly commented Mar 26, 2016

You can wrap any function that returns a Promise with asyncify and the returned function can then be used like any-other callback-style async function. (e.g. passed to map, waterfall, etc..)

@megawac
Copy link
Collaborator

megawac commented Mar 28, 2016

See #956 for discussion

@demian85
Copy link

Is there a way to make async control flow functions to return a promise instead of passing a callback node-style?
For example, the following code:

exports.getUser = function (userId) {
  return new Promise((resolve, reject) => {
    db.collection('users').findOne({ _id: userId }).then((user) => {
      async.map(user.contacts, (contactId, cb) => {
        db.collection('users').findOne({ _id: contactId }, { name: 1, email: 1 }).then(callbackify(cb));
      }, (err, contacts) => {
        if (err) return reject(err);
        getTeams(userId).then((teams) => {
          const output = Object.assign(user, { contacts, teams });
          resolve(output);
        }).catch(reject);
      });
    }).catch(reject);
  }).catch(handleError);
};

can be changed to:

exports.getUser = function (userId) {
  return db.collection('users').findOne({ _id: userId }).then((user) => {
    return async.map(user.contacts, (contactId, cb) => {
      db.collection('users').findOne({ _id: contactId }, { name: 1, email: 1 }).then(callbackify(cb));
    }).then((contacts) {
      getTeams(userId).then((teams) => {
        return Object.assign(user, { contacts, teams });
      })
    })
  }).catch(handleError);
};

Am I wrong? It's not cool mixing callbacks and promises, I just want to use promises and async controls utilities without much boilerplate.

@megawac
Copy link
Collaborator

megawac commented Nov 25, 2016

@demian85
Copy link

demian85 commented Nov 26, 2016 via email

@aearly
Copy link
Collaborator

aearly commented Nov 26, 2016

See the discussion in #956 . We still support envs where there aren't native promises. Use something like pify and you can manage it yourself.

@Enelar
Copy link

Enelar commented Aug 14, 2018

https://www.npmjs.com/package/google-spreadsheet use that in doc, took me a while to understand there is no promises involved lmao, have a great day everyone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants