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

Node.js Async Function Best Practices #6

Open
gergelyke opened this Issue Oct 23, 2017 · 6 comments

Comments

5 participants
@gergelyke
Owner

gergelyke commented Oct 23, 2017

No description provided.

@gergelyke gergelyke added the comments label Oct 23, 2017

@gergelyke

This comment has been minimized.

Owner

gergelyke commented Oct 25, 2017

@ksmithut thanks, fixed it!

@xkizer

This comment has been minimized.

xkizer commented Oct 26, 2017

async function main () {
  const [user, product] = await Promise.all([
    Users.fetch(userId),
    Products.fetch(productId)
  ])
  await makePurchase(user, product)
}

can also be written as

async function main () {
  const user = Users.fetch(userId)
  const product = Products.fetch(productId)
  await makePurchase(await user, await product)
}
@slavaGanzin

This comment has been minimized.

slavaGanzin commented Oct 26, 2017

You do await + callbacks. "Best practices"? For real?

@ksmithut

This comment has been minimized.

ksmithut commented Oct 26, 2017

@slavaGanzin If you're referring to the express example, express uses callbacks exclusively, no way around it (ATM). So for express, yes, if that's what you're referring to.

@slavaGanzin

This comment has been minimized.

slavaGanzin commented Oct 30, 2017

@ksmithut No, I'm talking about last example from this article:

const util = require('util')
const async = require('async')
const numbers = [
  1, 2, 3, 4, 5
]
mapLimitAsync = util.promisify(async.mapLimit)
async function main () {
  return await mapLimitAsync(numbers, 2, (number, done) => {
    setTimeout(function () {
      done(null, number * 2)
    }, 100)
  })
}
main()
  .then(console.log)
  .catch(console.error)

Callbacks, promisified callbacks, await. This code is utter mess

@phelma

This comment has been minimized.

phelma commented Nov 1, 2017

The @sindresorhus promise-fun repo has a bunch of good promise helper modules, as an alternative to using the async module.

https://github.com/sindresorhus/promise-fun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment