jhusain / compositional-functions

137 stars 7 forks source link

Tasks vs Promises with async/await #7

Open trusktr opened 9 years ago

trusktr commented 9 years ago

You mention in the README that

This makes it easy to create Task sequences that can be cancelled.

How does that verbose and hard to learn syntax compare to just awaiting async functions in a loop, then simply breaking the loop in order "to create Task sequences that can be cancelled"?

For example, assume some tool called TaskRunner, written with ES2016 async/await in mind (all promise based):

// app.js -----------------------------------------------
import TaskRunner from './TaskRunner'

let tasks = new TaskRunner()

tasks.run([
    async function() {/* ... */},
    async function() {/* ... */},
    async function() {/* ... */},
    async function() {/* ... */},
    async function() {/* ... */},
    async function() {/* ... */},
    async function() {/* ... */},
    async function() {/* ... */},
    async function() {/* ... */},
    async function() {/* ... */},
    async function() {/* ... */},
    async function() {/* ... */},
])

setTimeout(() => tasks.stop(), 1000) // stop the tasks in their tracks.

// TaskRunner.js -----------------------------------------------
export default
class TaskRunner {

    constructor() {
        this.shouldStop = false
    }

    async run(tasks) {
        let cancelled = false

        for (let i=0, len=tasks.length; i<len; i+=1) {
            if (this.shouldStop) {
                this.shouldStop = false
                cancelled = true
                break
            }
            else await tasks[i]()
        }

        return cancelled
    }

    stop() {
        this.shouldStop = true
    }

}

That (basically) does what you said, but is a ton more readable (of course, features can be added like error handling, pausing tasks, keeping track of running state and percent complete, etc).

Maybe I'm not seeing the benefit of the Tasks yet (thanks for writing about them for me to read about in the first place :smile: ).

trusktr commented 9 years ago

Besides, by merely having promises/async/await in the language, can't we simply implement the other mechanisms (tasks and observables) using async/await, so that the following is possible?

import observable from 'somewhere'

let getStockPriceByName = observable(async function (stockName) {
    let symbol = await getStockSymbol(stockName);
    let price = await getStockPrice(symbol);
    return price;
})

// Note, called without `await`:
let price = getStockPriceByName("Netflix");

let subscription = price.subscribe(price => console.log("The price of Netflix is ", price));