« first day (3162 days earlier)      last day (1779 days later) » 

1:44 AM
Morning!
 
mr5
weird grouping
 
2:00 AM
Hey Guys, I just posted a C# question if someone wants to get some points for an answer?
0
Q: .Net Core [Authorize] - Or instead of And for permission test

Tom CrosmanThe following code authorize filter passes if the user is BOTH a role of "admin" AND a Policy of "New York". How do i change it to "admin" OR "New York" [Authorize(Policy = "New York", Roles = "admin")] I want an OR statement in this specific case, not an AND.

 
2:40 AM
can someone explain Task.FromResult ... is this like saying cut the promise short and give me the result now?
 
mr5
@ChristianMatthew yes
I'm not sure what you mean by "cut the promise short"
 
wow that's pretty cool
well in javascript promises are async programming
one of the most frustrating things in javascript about async programming is not being able to get a value from the promise chain
 
mr5
I've never experienced working with futures and promises
 
mr5
in any language that supports it.
maybe future/promises evolved in async/await?
I've heard they're quite similar
 
Rob
3:05 AM
@ChristianMatthew No, it's putting a value into a task. It's the same as Promise.Resolve(1)
var task = Task.FromResult(1); var result = await task; // result has the value 1
 
@Rob so why wouldn't you just use the await?
 
Rob
What do you mean?
 
in this example
https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.fromresult?view=netframework-4.8
private static Task<long> GetFileLengthsAsync(string filePath)
 
Rob
Yep.. still not following though
You use Task.FromResult when you need to return a task, but you've already got the value
Imagine you had a function:
 
i think it is the you've already got the value part
 
Rob
3:16 AM
Task<string> DownloadWebpage(string url) {
	if (inCache(url)) {
		return Task.FromResult(cache[url]);
	} else {
		return await DownloadAndCache(url);
	}
}
 
2 things
 
Rob
Obviously pseudocode.. but here, we either have the value already, or we need to go and do some work to get it
The caller doesn't care, though, they still await the result of DownloadWebpage()
 
can you explain already have the value
 
Rob
Sure
Someone called DownloadWebpage. But we've already cached the content for that URL
So we can just return that content without downloading it again
 
Rob
3:18 AM
But if we don't have it in the cache, we do need to kick off a task to get it
 
how do you know it's cached
oh lol the if statement
 
Rob
Because there's code to cache it... this is unrelated to tasks
 
that is not the same as promise resolve
 
Rob
Sure it is. Promise.Resolve(1) creates a promise that will output 1 when awaited, or when .then()'d
 
lol i can see that comparison
here is my slight confusing
confusion... in javascript in a promise you don't expect there to be a resolved portion of an answer.
you run the promise and get a result
i.e. await for it
or resolve it
but the function has to be run
 
Rob
Sure you do. That's why that function exists
A promise doesn't mean you need to run a function
It's a wrapper saying it "promises" to give you a result or an error sometime in the future
 
yes but in this scenario you are running an asynchronous task and if you have result you want you just return that.
 
Rob
"Sometime in the future" is probably better expressed as >= now
 
in javascript it does in any meaningful sense
you run a function that is promise and it returns the result of that function
 
Rob
3:24 AM
Sure, but not always
Otherwise you'd just pass function pointers around
 
i've never seen any promise do anything otherwise
 
Rob
That's not evidence that it doesn't :)
 
lol true
 
Rob
One could easily write the above DownloadWebpage in javascript
And if it's cached, you for sure would use Promise.Resolve rather than downloading it again
 
but still you can't early exit a promise you can set the resolve but you can't obtain a value from outside the promise
you can't
 
Rob
3:25 AM
Huh?
 
you have to resolve a javascript promise correct?
 
Rob
Sure
I mean... there's nothing stopping you from not
 
ok i can't scope to a value outside of that promise function correct?
 
Rob
But yes, usually, you resolve it with a value
Why not?
 
and it be the promise value?
 
Rob
3:27 AM
return new Promise((resolve, reject) => resolve(window.location))
 
but why not use await?
 
Rob
What are you awaiting?
 
hold on
i am going to build something in jsbin
 
3:55 AM
ok
function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}

var test = async () => {
	 var x = await resolveAfter2Seconds(12);
   console.log(x);
   return x;
}

console.log(test());
for this to work everything has to be done inside of the test function
i can't use the value of the return anywhere else
do you agree with that?
 
Rob
Of which return?
 
the text function
test*
 
Rob
Still not following.. which value do you mean you can't use?
 
i can't use the value from test... the return x
 
Rob
Sure you can
If your last line were console.log(await test());
It would log 12 twice
console.log(test()); is currently logging the promise returned by test
 
4:05 AM
not legal
console.log(await test());
 
Rob
I just tried it in my console.. it's most definitely legal
And as expected, it logged 12 twice
 
function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 4000);
  });
}

var test = async () => {
	 var x = await resolveAfter2Seconds(12);
   console.log(x);
   return "Christian";
}

console.log(test());
ok fair enough however
I can't use test to return anything unless I await the promise to finish
right
do you agree with that
 
Rob
Nope
You can remove the await entirely and just write var x = resolveAfter2Seconds(12)
 
var test = () => {
	 var x = resolveAfter2Seconds(12);
   console.log(x);
   return "Christian";
}

console.log(test());
i could remove the async and await and get the return yes. at this point there is no promise and I am not yeilding the x at any point
 
Rob
You are. x is now the promise, not the value returned by the promise
 
4:17 AM
but I can't get to it
show me
there is no resolving that promise
 
Rob
No, it will resolve. But nothing is listening to the promise to grab the value
That's to be expected though, isn't it?
 
you are right
but ok the syntax and purpose matters right
 
Rob
You could say the same thing of any variable. If you assign a variable to something and then never use it again.. then yes, nothing can access it in the future
 
ok so now I want to get my bearings straight for the Task object
 
Rob
It's essentially the same thing as a javascript promise
 
4:19 AM
 private static Task<long> GetFileLengthsAsync(string filePath)
 
Rob
It's a wrapper around a concrete value or a task to return a value
 
this here is not async correct
 
Rob
Why not?
 
because there is no async designation
 
Rob
You mean there's no keyword async?
 
4:21 AM
yes so it is not a promise
 
Rob
Nope. Does your function resolveAfter2Seconds have the async keyword?
No, because it's just returning a promise. It's not awaiting anything
GetFileLengthsAsync just returns a task. It's not awaiting anything
The person using GetFileLengthsAsync will need to await the task to get the value out
 
if I did this
private static async Task<long> GetFileLengthsAsync(string filePath)
what does that imply exactly
 
Rob
It means that GetFileLengthsAsync needs to await a task in its implementation
And.. instead of doing return someTask;, it'll need to return await someTask
Because C# has a bit of magic that wraps anything returned into a task
 
ok so but in this function it does return a promise
function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 4000);
  });
}
ok so
when you don't use async in the syntax
you dont' need to return await you can just return
is that fair
i have one more question about base contructors if you dont' mind
i think i get it...
it essentially is creating a function like this that is returning a resolved promise
function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}
is that fair?
which is what i think you said in the beginning
 
Rob
4:46 AM
@ChristianMatthew Not sure what you mean?
That resolve after 2 seconds isn't a resolved promise. It's a promise which is resolved after 2 seconds
 
i get that
what i am specifically is this... when you use a task without an async designation the return isn't an awaited one
 
Rob
If you haven't marked the method async, you cannot await anything. You'll get a compiler error
 
so the Task.FromResult() will create a Task<Tresult> which is eventually a resolved promise
 
Rob
It's immediately resolved
 
would you call resolveAfter2Seconds(x) immediately resolved?
 
Rob
4:51 AM
No
it resolves after 2 seconds
I think you're getting mixed up with await here.
async Task<int> DoSomething()
{
	return await GetValue();
}
 
ok exactly
 
Rob
DoSomething() will create a task, which itself will await the value of another task
 
i get that
but in the other case it will create a task that is not waiting for anything as you said it immediately resolves
so why go throught process of creating a Task<TResult>
is it because it will be used by an async method elsewhere?
 
Rob
You mean, when would you use Task.FromResult()?
 
Rob
4:55 AM
Because the person calling your method doesn't know you have the value immediately
And if we go back to my caching example
Sometimes we do, sometimes we don't
The alternative, would be to return an object that had a pointer to the result or a pointer to a task
 
can you post that example again
 
Rob
But then the user would need a bunch of checks to see how to grab the value
But now, they can simply await the returned task. It doesn't matter that the task doesn't need to do any work
Task<string> DownloadWebpage(string url) {
	if (inCache(url)) {
		return Task.FromResult(cache[url]);
	} else {
		return await DownloadAndCache(url);
	}
}
 
don't you have to preface the 1st Task with async? just in case
 
Rob
Sorry, yes
It was mainly pseudocode
 
any one can help me out with this issue stackoverflow.com/questions/56540174/…
 
Rob
4:57 AM
Hangon, lemme fix it so it's actually correct :)
async Task<string> DownloadWebpage(string url)
{
	if (inCache(url))
	{
		return await Task.FromResult(cache[url]);
	}
	else
	{
		return await DownloadAndCache(url);
	}
}
You can also do the following:
Task<string> DownloadWebpage(string url)
{
	if (inCache(url))
	{
		return Task.FromResult(cache[url]);
	}
	else
	{
		return DownloadAndCache(url);
	}
}
 
else
	{
		return DownloadAndCache(url);
	}
lol you know this is driving me bonkers right
 
Rob
DownloadAndCache is a function that will return a task
So, if DownloadWebpage isn't async, it can just return that task
 
in that case of what I just posted... there is no purpose of the Task<string> designation
but it will still return that task
 
Rob
What do you mean?
DownloadWebpage is returning a task which will produce a string when awaited
 
im pretty sure it is what your saying... the
else
	{
		return DownloadAndCache(url);
	}
is not a promise in anyway at the end of the day
 
Rob
5:02 AM
No, it is
The result of DownloadAndCache is the promise
await DownloadAndCache() is creating a new 'promise', which itself is waiting for the result of the promised returned from DownloadAndCache
If you don't await, then you're just returning the promise returned from DownloadAndCache
 
ok but that is what i am saying is different in javascript
 
Rob
Nope
 
you can't just arbitrarily say when something is a promise or not
 
Rob
I believe javascript's promises can be flattened together, but the principal is the same
 
how do I know DownloadAndCache(url) is a promise?
 
Rob
5:04 AM
Because it returns a Task<string>
 
huh
if I say this:
Task<string> someMethod(string b) {
return "dog";
}
 
Rob
You can't
 
your saying that returns Task<string>
 
Rob
No, that won't compile
You need to return a Task<string>, but you're returning a string
 
what makes DownloadAndCache(b) different
 
Rob
5:10 AM
It returns a task which produces a string
 
lets back up for a second... a Task is just running tread at a moment in time right
 
Rob
Threads aren't necessarily involved
If you understand promises, then it's the same as a promise
 
of course they have to be in some capacity that is where the information is stored
 
Rob
I know you think they're different, but they're really the same concept
 
locally
 
Rob
5:14 AM
The differences are irrelevant
No, they do not need a separate thread
 
im agreeing on that I am not arguing that I am trying to get this syntax down for c3
 
Rob
You can think of a task as a container that has two properties
1. The result. This might be null, if the task is still executing
2. A pointer to a function to execute
When someone awaits the task, the task will check if the result is there
If it's not there, it'll say "Nope, you've gotta wait". Then, it'll wait until the function executes, and stores the result back in result, and tells the awaiter it's ready
You can think of this happening on a separate thread. But that's not actually true for the implementation
Task.FromResult(1) will just set the result property. There is no function/work to do
So when someone awaits that task, they get the result immediately
 
I said a Task exists on a particular thread
a Task belongs to a thread
 
Rob
That's not the case either
 
how? that's impossible
 
Rob
5:19 AM
A task can continue on an entirely different thread
 
that is how this all works.
 
Rob
Javascript is single threaded, so clearly it's not impossible :)
 
lol
javascript is single threaded and a promise still waits for code to be finished before it resolves on that single thread
what do you do by the way
 
Rob
5:39 AM
You mean for work?
 
Rob
I'm a tech lead
Though... I end up developing most of the time because.. well.. our company doesn't spend enough time as it should designing things as it does just implementing them the quickest way ;)
 
have you ever thought to do your own thing
 
Rob
Sure... when I have spare money to support it and have a decent idea
 
where are you from
 
Rob
5:43 AM
Australia
 
i have a very good idea and there is lots of money here in the US for good ideas
lol there's lots of money for bad ideas
why do you think you need money to get started on an idea
 
Rob
Because if I were serious about it, I'd need to work full time on it
 
would you let other people code it or would you want to code it by yourself for the beginning parts
 
Rob
I wouldn't have money to pay them, so..
 
with a prototype you can get money
here in the us anyways
 
6:00 AM
Good morning
You'd need to build a prototype
 
6:15 AM
@Rob what would you think about one of your devs being in a StackOverflow chat room? I can wonder what my boss would think all I want, real answers need real sources.
 
Rob
I don't have any devs ;)
 
i think it's awesome
 
Rob
But if I did, I wouldn't care whatsoever. As long as the work gets done, they'd be free to do as they wish
Which is pretty much the culture here already
 
so @Rob when Task is used without async it is still asynchronous programming which means non-blocking?
 
yes
a task runs on its own
it'll never block your main thread directly
The nice thing about tasks is, you don't have to worry about threading. The framework does threading for you.
 
Rob
6:21 AM
It can, in some cases
 
directly?
Indirectly by keeping some ressource, absolutely. But directly?
 
Rob
Sure, it can just put in a thread.sleep. Tasks don't need to have multiple threads
Or the classic .Result deadlock depending on your synchronisation context
 
the point of ansynhronous programming is to be on a single thread
 
No
The point of asynchronous programming is you dont have to worry about threads as much, and it still workds on a single thread
 
true ok
 
Rob
6:24 AM
async is completely unrelated to threads
The implementation may use threads, but they're different concepts
 
ahhh i think i get it.
the reason why you use Task in your asynchronous programming without necessarily an async await method is because you don't want to block any thread in the process of an asynchronous coding pattern?
 
Rob
There's no blocking involved
The reason you use await is because you want the result of the task
If you don't want the result, in the case of the caching example, you can just return the task so someone who does want the result
 
without blocking?
 
Rob
Yes
The execution is deferred, but nothing is blocked
 
i am just saying the difference between doing that and a regular method execution
 
Rob
6:30 AM
When you await something, the state of your execution is saved and passed up the chain
Eventually, whatever's responsible for scheduling the task, will continue the execution, which may be on another thread
 
ok and as you were saying before when you await something the task is waiting or paused until that method has completed.
 
That context is paused
 
Rob
Await does nothing to the task you're awaiting
 
@MikeTheLiar I have no idea what you mean by this, sir.
 
The threading executing that leaves the context and does something else with its precious time
 
6:31 AM
await does nothing to the task you're awaiting? huh
 
So, I stabbed myself in the hand this morning
 
just waits for it to finish
 
Rob
await says "Save my state until the result of the task I'm awaiting is ready to give me a value"
 
The cat food can wouldn't open and I (smartly) decided to push a knife through the lid, and it went through the can and into my hand
 
Rob
When the task you're awaiting finishes, your state will sometime in the future be restored, and execution will continue
 
6:32 AM
just the tip though fortunately
 
yes that's what i am saying
async Task<string> DownloadWebpage(string url)
{
	if (inCache(url))
	{
		return await Task.FromResult(cache[url]);
	}
	else
	{
		return await DownloadAndCache(url);
	}
}
 
Genius
 
ikr?
 
the task downloadwebpage is waiting for one of those methods to be complete
 
@Neil Youch.
 
6:34 AM
is that wrong?
 
I didn't bother mentioning that part to my wife.. just the part that explained the cat food on the wall
 
Rob
It's correct, as long as you understand that it doesn't block anything
Whoever called DownloadWebpage() may continue its execution after calling it, until they await the result
 
that's the most important part right is that it is non-blocking
 
Rob
Console.WriteLine("Here!")
var downloadTask = DownloadWebpage("something");
Console.WriteLine("Still here, very likely to be before the download is completed")
var payload = await downloadTask;
Console.WriteLine("After the download has been completed")
From the caller of DownloadWebpage's perspective
 
ok I think I have the hang of it... in javascript it's more purposeful on something that is a promise. you know what it is and you deal with it.
 
6:38 AM
how do you guys suppose async/await is implemented backend? is it some sort of blocking queue with one element I suppose?
 
Rob
It's turned into a state machine
 
if you wanted to have a await/async type behavior without the keyword, I suppose that's how you'd do it
 
Rob
Which is a way to do coroutines in c#
 
here you have this abstraction of asynchronous programming that is not async await... but like you said I can return an immediate result.. i.e. Task.FromResult() and obtain a non-blocking result... that is probably passed along and used in asynchronous methods elsewhere
 
nothing about tasks is blocking, why always mention things being non-blocking?
 
6:41 AM
@Squirrelkiller because I can't figure out why Task would be used... why not just not use task for a result you know you have
when you're forcing Task you're forcing asynchronous programming
 
Rob
Because you can't return union types in c#
 
You mean return Task.FromResult(5); instead of return 5;?
 
Rob
You can return a wrapper that points to a task or a value
But that's messy
 
and thus a non-blocking event is what you're looking for
 
Rob
And a leaky abstraction
 
6:42 AM
yes
 
Rob
Rather... if you might return an actual task, then it's nicer to wrap constant values in a task
That way, the user doesn't need to know the implementation details. They just await the task. Whether the task is immediately resolved or not is irrelevant
 
but is it enough to implement an async solution in a situation where you currently don't require it but possibly may need it in the future?
 
Rob
If your method was only returning constant values, then of course, you wouldn't use tasks
 
this is for efficiency right... it doesn't have to be done
 
not even if that may change?
there is an overhead doing it that way I assume
 
Rob
6:45 AM
Sure. In my caching example, you could change the return type to object. Then return the content or task directly
But then the user needs to know what can be contained in 'object' and cast the result
 
  private static Task<long> GetFileLengthsAsync(string filePath)
   {
      if (! Directory.Exists(filePath)) {
         return Task.FromException<long>(
                     new DirectoryNotFoundException("Invalid directory name."));
      }
      else {
         string[] files = Directory.GetFiles(filePath);
         if (files.Length == 0)
            return Task.FromResult(0L);
         else
            return Task.Run( () => { long total = 0;
                                     Parallel.ForEach(files, (fileName) => {
 
Rob
@Neil Depends how likely it is to change, I suppose
 
good point
 
Queues the specified work to run on the ThreadPool and returns a task or Task<TResult> handle for that work.
 
you can always convert to async later I suppose
 
Rob
6:47 AM
Ah... not so easily
Once you go async, you need to be async all the way up
Unless you're doing something like GetAwaiter().GetResult()
But then.. why bother
 
well at that point, you'd ensure there were a nice facade or interface at which you can cleanly divide between sync and async
 
the reason why I am saying thread and non-blocking is because the reason why you go through this trouble is to not create bottle necks and have the code execute efficiently
 
you wouldn't convert half your project to async
once you know a task is async, it doesn't have to necessarily call another async task and use await on it
 
again, the thread is important here and in JS it's the only one you got
 
depends :P
though client-side the division is well-defined. You can't accidentally spawn 20 threads without realizingi t
 
Rob
6:50 AM
Nothing to do with bottlenecks
 
depends on what... the whole thing would be brittle if you were creating thread pools off and on all the time. how would state be managed without enormous overhead
 
"worker" threads
 
Rob
And if I were to have a guess, that implementation with threads won't be useful
Unless the paths are on separate physical drives
 
i get the framework handles this but it is mostly trying to maintaing threads for darn sure by class and methods executing
 
@ChristianMatthew depends.. js handles multi-threading, that's what I mean
 
6:51 AM
GoOoOoOoOoOoOoOoOoOoOoOd Mornin' pleberinos!
 
Hey ho!
 
Let's go!
 
Rob
@ChristianMatthew No, the reason for tasks/promises is to represent the idea that you know how to get the answer, but it will take some time to do so - usually waiting for non cpu-bound work to complete
 
non-cpu bound work?
 
ohayou
 
Rob
6:54 AM
Yes, like reading from disk or from the network
 
Morning!
 
@Rob is the reason why you keep saying the thread doesn't matter is because of thread safety
 
Rob
No
 
Something like "I cannot throw CPU at it to solve it, we must wait"
 
Rob
I'm saying the thread doesn't matter because threads are an entirely different concept from async/await
 
6:58 AM
The Task Parallel Library (TPL) is based on the concept of a task, which represents an asynchronous operation. In some ways, a task resembles a thread or ThreadPool work item, but at a higher level of abstraction. The term task parallelism refers to one or more independent tasks running concurrently. Tasks provide two primary benefits:
 

« first day (3162 days earlier)      last day (1779 days later) »