Giter Site home page Giter Site logo

Task vs Promise about fable HOT 6 OPEN

dbrattli avatar dbrattli commented on June 13, 2024
Task vs Promise

from fable.

Comments (6)

MangelMaxime avatar MangelMaxime commented on June 13, 2024 1

Are Task in .NET hot or cold? In the sense do they execute on creation or wait to be invoked?

Could it lead to problems if Task are cold in .NET and/or Python, while in JavaScript promise are hot ?

While we decide on what to do here, a possible workaround for people who need to add cross platform asynchronous code is to create a module which encapsulate the compiler directives. Could benefit from helpers functions too.

Note: Code below use async but I believe the same could be applied to Task

module Main

open Fable.Core

// We need the `<'T>` here as it allows to mark the function/variable inline for better output.
let inline crossAsync<'T> =
#if FABLE_COMPILER
    promise
#else
    async
#endif

type CrossAsync<'T> =
#if FABLE_COMPILER
    JS.Promise<'T>
#else
    Async<'T>
#endif

let hello (name : string) : CrossAsync<string> =
    crossAsync {
        return "Hello " + name
    }

// Fake the API being called by the consumer code
crossAsync {
    let! hello = hello "World"
    printfn "%s" hello
    return ()
}
#if FABLE_COMPILER
|> Promise.start
#else
|> Async.Start
#endif

from fable.

MangelMaxime avatar MangelMaxime commented on June 13, 2024 1

Seems like in .NET there is an exception when you use the constructor to create the task.

Tasks that are created by the public Task constructors are referred to as cold tasks... All other tasks begin their life cycle in a hot state.

Source

But this is something we can probably ignore (will need some testing so see how often this "problem" occurs).

If I am reading you correctly, then we have:

  • .NET task are hot
  • Python scheduled futures are hot
  • JavaScript promise are hot
  • Rust ? @ncave What are your plans for supporting Task in Rust? Does Rust have hot task?

If all the these major targets have the same hot behaviour available natively, I think it could makes sense to try implements an equivalence between them. This would allows, people to share the same code but also to use the native APIs.

Currently, as you mentioned in your first message, we have these situations:

Situation 1

People prefers to use native primitives and so use Promise for JavaScript and Async for .Net.

Then if they need to merge the code, they needs to use compilers directives or helpers like Async.AwaitPromise / Async.StartAsPromise

Situation 2

People value more shared code and write everything using Async which are natives to .NET but not to JavaScript for example.

So implementing Task by making them use the native APIs would create a third situation which is the best of both world:

Ability to use native F# API which is standard in F# so shareable and use native APIs in the runtime.

from fable.

ncave avatar ncave commented on June 13, 2024 1

@dbrattli

Could we make Giraffe run on Rust?

I guess the first step would be to make the TestTask tests pass.

from fable.

dbrattli avatar dbrattli commented on June 13, 2024

Tasks in .NET are hot in the sense that when you hold a Task returned from an async method then it's already running regardless if you use await or not. So Task in .NET and Promises in JS are both hot. Tasks in Python are scheduled Futures so they are hot. This is also why I'm unsure about rewriting all Task based code to use F# async for interoperability.

The workaround is nice if you own the codebase but e.g for Fable.Giraffe I want users to use their existing Giraffe webapp without having to modify their code. I.e they can decide if they want to run their web server on .NET, Python or Node (or a combination). Then it would be nice if we could call a Task for a Task.

from fable.

ncave avatar ncave commented on June 13, 2024

@MangelMaxime

What are your plans for supporting Task in Rust?

There is already some partial support for Async/AsyncBuilder, Task/TaskBuilder, Thread/ThreadPool/Monitor added by @alexswan10k.

Does Rust have hot task?

Afaik Rust's futures are cold, but I'm not sure that is a showstopper.

@dbrattli

It's currently hard to use Fable to write code that uses Task

Can you please elaborate, just trying to understand what the stated goal is.
Is it to get most of the languages (JS/TS/Rust/Python) to support at least what is needed to pass the TestTask tests?

from fable.

dbrattli avatar dbrattli commented on June 13, 2024

Yes, started porting Fable.Giraffe to TS/JS. It already runs on Python so the current Task tests would solve my needs. Could we make Giraffe run on Rust? 🙈

from fable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.