Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Task equivalent to Promise.then()?

With the addition of async / await to TypeScript using Promise(s) can look very syntactically close to Task(s).

Example:

Promise (TS)

public async myAsyncFun(): Promise<T> {
    let value: T = await ...
    return value;
}

Task (C#)

public async Task<T> MyAsyncFun() {
    T value = await ...
    return value;
}

I was wondering if the other way around, there was an equivalent to .then() for Task(s).

Example:

Promise (TS)

Promise<T> promise = ...
promise.then((result: T) => ...do something...);
like image 372
William Lohan Avatar asked Dec 15 '16 19:12

William Lohan


People also ask

Is Task same as promise?

Promise is a well-defined built-in object that makes it easy, among other things, to chain synchronous or asynchronous operations. Task is an object defined in trace_viewer/base that can be used to chain synchronous operations. Since both tasks and promises allow chaining operations it's easy to confuse them.

What is a promise in C#?

Promise : Which waits indefinitely for the result. TimedPromise : Which waits for the result only till the specified time. If result is not available with in the time, it throws timeout exception.

Do async functions return a promise?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. Note: Even though the return value of an async function behaves as if it's wrapped in a Promise.resolve , they are not equivalent.

Is .then async?

The difference is that in an async function, JavaScript will pause the function execution until the promise settles. With then() , the rest of the function will continue to execute but JavaScript won't execute the . then() callback until the promise settles.

What is the difference between a task and a promise?

It cannot be that Task is used here to nicely keep track of the possible error, I use Promises for error values the same way. The order of arguments is different: Task expects the error handling callback at first position, while Promise puts it at the second one. But that is a tiny difference. What is really different is when an operation runs.

What is the use of then () method in promise?

The then () method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise. Note: If one or both arguments are omitted or are provided non-functions, then then will be missing the handler (s), but will not generate any errors.

What are promise tasks in JavaScript?

As a reminder, Promise Tasks are tasks that represent a kind of “event” within a system; they don’t have any user-defined code to execute. Task.Delay is the asynchronous equivalent of Thread.Sleep.

What happens when you throw an error in a promise?

throws an error, the promise returned by then gets rejected with the thrown error as its value. returns an already fulfilled promise, the promise returned by then gets fulfilled with that promise's value as its value. returns an already rejected promise, the promise returned by then gets rejected with that promise's value as its value.


1 Answers

I've used ContinueWith which can work if you have one or multiple Tasks running.

example:

public async Task<T> MyAsyncFun() {
    T value = await ...
    return value;
}

MyAsyncFun().ContinueWith(...

https://msdn.microsoft.com/en-us/library/dd270696(v=vs.110).aspx

like image 122
jdmdevdotnet Avatar answered Oct 16 '22 11:10

jdmdevdotnet