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

Pick a minimum version of Node.js (recommendation: node 10) #1584

Closed
stuartromanek opened this issue Aug 9, 2018 · 2 comments · May be fixed by qsays/apostrophe#29 or falkodev/apostrophe#37
Closed
Labels
v3 Related to Apostrophe 3

Comments

@stuartromanek
Copy link
Member

No description provided.

@boutell boutell changed the title Pick a minimum version of Node.js Pick a minimum version of Node.js (recommendation: node 10) Aug 16, 2018
@boutell
Copy link
Member

boutell commented Aug 16, 2018

Ludo and I tried an experiment and found that node 10 can iterate asynchronously over an array, i.e. you can use await to replace Promise.eachSeries or async.eachSeries. This is a really nice win over using for (let i = 0; (i < things.length); i++) with await, although that would have been adorably old-fashioned.

And, node 10 becomes an LTS in October... before we ship 3.x.

So, I believe this question has been answered: the minimum version of Node for 3.x should be Node 10.

@boutell
Copy link
Member

boutell commented Aug 17, 2018

This is cool: It turns out that even node 8 can iterate over an array while await-ing the work that has to be done on each array element, without using an old-school incrementing for loop counter. See below. This is welcome news because we do this a lot and for await is a little intimidating for something so common.

However we are still likely to find uses for actual async iterators in node 10, such as iterating over streams.

https://codepen.io/boutell/pen/oMKwbB?editors=0012

// Proof of concept: "for ... of" can iterate over array
// elements while doing async work on each element,
// usinmg "await", even in node 8. We don't need to
// wait for node 10 and "async iterators" for that, 
// although those do cool things too.

// This will always print the products in this order,
// even though they take a variable amount of time to "sell".
// This shows they are being awaited in sequence, one
// at a time, which is what we want.
//
// (Yes, there are cases where you want stuff to happen
// in parallel. For that Promise.all is the way to go)

const products = [
  'cheese',
  'trees',
  'cats'
];

// Do some asynchronous work to sell a product

async function sell(product) {
  await wait();
  console.log('sold ' + product);
}

async function main() {
  for (const product of products) {
    await sell(product);
  }
}

main();

// This is just a simple promise-ification of setTimeout,
// which is callback-driven. We might use `Promise.delay`
// from bluebird or some lighter wrapper for this. But
// here timeouts are just an example and we'd probably
// be doing more interesting async work like talking to
// a database

function wait() {
  return new Promise(function(resolve, reject) {
    setTimeout(resolve, Math.random() * 1000);
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v3 Related to Apostrophe 3
Projects
None yet
2 participants