The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Use Task for Asynchronous Actions

Use Task for Asynchronous Actions

3:46
We refactor a standard node callback style workflow into a composed task-based workflow.
Watch this lesson now
Avatar
egghead.io

We refactor a standard node callback style workflow into a composed task-based workflow.

Avatar
Egghead

Hi, at the end of video you say one solution uses 'new' Task and other solution uses 'old' Task, can you elaborate a little bit, thanks.

Avatar
Brian Lonsdorf

Hi!

If it's the part I'm thinking of - where make app a function or not:

app = () => new Task((rej, res)...
app().fork

vs

app = new Task((rej, res)...
app.fork

The idea is the former is a function that creates a new Task object instance each time you call it and the other is just assigned to the Task instance itself.

Since Task is lazy, it doesn't actually run until we fork it. That means we don't need to put it in a function wrapper to prevent it from running.

In reply to Egghead
Avatar
Egghead

Hi Brian,

Thanks for response.

I understand now, other question that I have is you could use Promises, right, instead of 'Task' library.
Is there difference between those two, because I use Promises in the same way as Task is used here.

In reply to Brian Lonsdorf
Avatar
Brian Lonsdorf

Promises are eager, which means they are not pure - they will immediately run side effects upon construction. Task will allow you to work with values as though they were there, but it does not run until you tell it with fork() - usually outside your pure application.

In reply to Egghead
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?