lukeed / taskr

A fast, concurrency-focused task automation tool.
MIT License
2.54k stars 73 forks source link

support native async function #288

Closed stkevintan closed 7 years ago

stkevintan commented 7 years ago

since node v7.6.0 has native support of async function.

lukeed commented 7 years ago

Hi, thanks! I have my eye out for Native Promise performance. It still has a little ways to go before it's faster or more memory efficient than Bluebird. See #275 for a little more.

For Taskr itself, I'll only consider doing a full switch to Native Promises once they're as fast or faster than Bluebird.

However, if you are looking to use native AsyncFunctions inside your taskfile, you're definitely able to! The only gotcha is that you can't define the AsyncFunction within your taskfile and use @taskr/esnext ... because the esnext package will rewrite your async into generators. You may import them from other files though!

// taskfile.js (no `esnext`)
const _sleep = ms => new Promise(r => setTimeout(r, ms));
const sleep = async ms => await _sleep(ms);

exports.build = function * (task) {
  // yield your own AsyncFunction (Node 7.4+)
  yield sleep(2000);

  // yield normal Taskr stuff
  yield task.source(...).babel(...).target(...);
};
// taskfile (with `esnext`)
const sleep = require('async-sleep');

export async function build(task) {
  await sleep(2000);
  await task.source(...).target(....);
}

Let me know if you have any questions!

stkevintan commented 7 years ago

thx for u detailed reply. I'm using taskr-esnext now. but, the approach of this plugin will conflict with koa2.x. for example:

app.use(async function (ctx, next) {
  const start = new Date();
  await next();
  const ms = new Date() - start;
  console.log(`${ctx.method} ${ctx.url} - ${ms}`);
});

the await next() will be replaced to yeild next() which will cause an error when starting a livereload server. hope someone can find a nice solution to fixed it.

lukeed commented 7 years ago

@stkevintan No problem!

Yeah, that's what I meant by:

The only gotcha is that you can't define the AsyncFunction within your taskfile and use @taskr/esnext ... because the esnext package will rewrite your async into generators. You may import them from other files though!

I would recommend splitting your Koa-related scripts from your Taskr content. IMO, the taskfile.js should only contain Taskr-specific definitions.

If you can point me to a repo (or paste more code), I'd be happy to help you begin the separation.

stkevintan commented 7 years ago

@lukeed thank you, you're a nice man! I already handled it.

lukeed commented 7 years ago

Hahah no problem! Happy to help, lemme know~

stkevintan commented 7 years ago

by the way, how about using babel-register and this plugin transform-async-to-module-method

hzlmn commented 7 years ago

@stkevintan It is too heavy for such task. And we wrap in coroutine internally so no need for this, if i understand you correctly.

UPD. I am for taskr-esnext transformation updates, but not in such manner. In any case if you know some edge cases it works as you expect and blazing fast.

lukeed commented 7 years ago

Yes, what @hzlmn said. You can also see my answer here (https://github.com/lukeed/taskr/issues/276#issuecomment-311259617) to the same idea. We tried it in the past --- didn't work well 😄

@hzlmn I have some import->require changes I can port from another project. Is that all we need?

hzlmn commented 7 years ago

@lukeed I guess yes, it is one missed feature for now i think.

lukeed commented 7 years ago

Version 1.1.0 ships with import syntax 🎉