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

Tasks vs Promises with async/await #7

Open
trusktr opened this issue Nov 25, 2015 · 1 comment
Open

Tasks vs Promises with async/await #7

trusktr opened this issue Nov 25, 2015 · 1 comment

Comments

@trusktr
Copy link

trusktr commented Nov 25, 2015

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 😄 ).

@trusktr
Copy link
Author

trusktr commented Nov 25, 2015

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));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant