« first day (4734 days earlier)      last day (439 days later) » 

03:19
@OlegValteriswithUkraine thanks for the logical explanation
 
3 hours later…
06:11
From observing the TS questions on SO, seems one of the hardest things for new TS coders, including users with five digit rep, is the concept that something could be null and that they have to handle this case. I'm baffled at the amount of times "use a null guard" is the answer to a question. It's something that shows up so often it might be a good candidate for a duplicate yet I don't feel it an appropriate FAQ. "If something could be null, do an if check to prevent that case"
 
4 hours later…
09:46
Since Im learning Promises in JS, I want to confirm what I'm thinking is right. I know what then function is for but I'm not confirm when the promise is exactly created.. So please correct me if I'm wrong:
"promise's then function registers the callback(s) and that is when it creates a new promise (that means the promise is in Pending state) and returns it .. like to the chained then"
With that said, it means :
10:01
var p = Promise.resolve( 21 );
p
.then( function(v){
console.log( v ); // 21
// fulfill the chained promise with value `42`
return v * 2;
} )
// here's the chained promise
.then( function(v){
console.log( v ); // 42
} );
Is this how it happens? : Promise.resolve(21) will create a new promise and immediately resolves it (asynchronously). The moment the promise is created and returned, (first)then function registers the associated callback. And the new promise is created and returned and passed to (second) then. Where it registers another callback. As first promise is resolved to 42, it calls the first callback to log 21 and after that second callback gets resolved and logs 42
10:21
Correction: As first promise is resolved to 21, it calls the first callback to log 21 and after that second callback gets resolved and logs 42
10:45
Promise.resolve() creates the resolved promise but handles the resolution (firing all then/catch/finally handler) in the next tick. So, even though you start with a resolved promise, it all still works asynchronously. It essentially doesn't matter if you have to wait for the value or not - you'd get the exact same resolt in the end, except on one case you might need to wait longer.
But to answer your question more concretely - Promise.resolve(21) would (as I said) create a promise but only fire the callbacks later. So, the first and second then() are still executed before that point. Thus the entire promise resolution chain is there and ready. Once the code runs to completion in the current cycle of the event loop, the next item in the queue would be picked up.
In this simple application, the next one would be firing the callback(s) of p which would then trigger the downstream promises to also resolve (as they depend on p) and then their callbacks would be resolved.
11:27
@VLAZ Thanks for the explanation. It seems my understanding is fine that i have wrote.
That at the time of registering the callback, then() creates a new promise to chain calls
That's correct. p2 = p1.then(something) ends up with two promises.
You can attach multiple then callbacks to one promise: p1.then(something); p1.then(somethingElse) and both something and something will be fired when p1 resolves.
Yes ..
 
3 hours later…
14:49
@VLAZ I believe it's a case of a much larger problem of inability to comprehend that a value can be missing when accessed. We wouldn't have so many NPEs otherwise
15:08
@OlegValteriswithUkraine I admit I've had the same problem when I began programming. But I'd expect somebody with 27k rep to not really struggle with this concept.
i think people just expect types to fix it somehow
new users to typescript sometimes seem to think it can do more than it does
and trust in it 100% for incoming data from the user or from an api, then get errors in prod
I do consider "why didn't my random type assertion also fix the data" as a separate class of problem.
I saw an answer today that claimed type assertions prevented runtime problems. I think it was AI generated but still. It's a really stupid claim to make.
For clarity, the context was code like this:
const foo: bigint = 0 as any as bigint;
there would definitely not be any runtime error if you do 42n + foo
tbf i too had this misconception very early on with typescript, but it didn't last long. it was more a case of... i have this incoming object from a json response and i need to use it within this class... how do i type it
that turned into how do i create json templates, and a whole useless rabbithole
 
4 hours later…
19:14
19:39
var p = Promise.resolve( 21 );
p.then( function(v){
console.log( v ); // 21
return new Promise( function(resolve,reject){
resolve( v * 2 );
} );
} )
.then( function(v){
console.log( v ); // 42
} );
!!> 42
@KevinB 42 Logged: [ ] Took: 0ms
second then() already has registered the callback to the promise. But (return new Promise(){...}) returns another new Promise
so second resolve i.e. resolve(v * 2) will get resolved right where it is and value 42 will be passed to second then() to log 42
So my Question: second then() first registered the callback for the promise which was created by the first then(). But new promise overwrote the first promise or what??
well
it's more a case of what .then does
if you return a promise to .then, .then will return that promise rather than creating a new one.
eh
that explanation isn't clear/100% accurate i guess
tldr, the callbacks are registered immediately, but what gets passed to the callback is dependant on how the previous promise resolves and what the previous callback returns
19:55
I have read in the Book YDKJS: "Every time you call then(..) on a Promise, it creates and returns a new Promise, which we can chain with."
yes
you can effectively say you have 4 promises at work in your given code
at any one of those points, you could have stored that promise in a variable and created entirely separate chains of promises
yes
@Sadiq Promises auto flatten. Returning a plain value and returning a promise from a .then() is effectively treated the same and the next .then() handler in the chain will only ever get a plain value, never a promise.
@VLAZ Agreed! but where is the new promise returned to => return new Promise () ...
the .then callback
what you return to .then is passed along to the next .then as an argument, unless it's a promise in which case the result of that promise is passed along to the next .then as an argument.
20:09
that means, second .then() registers the same callback with both the promises returned from:
1. first .then()
2. new Promise
it's more, the second .then's callback returns and is "done" when it returns the new promise.
it's just that promise that the .then initially returns won't resolve until the promise the callback returned resolves
keep in mind, that new promise coulda instead been an entirely different .then chain, so it wouldn't have resolved until all of those resolved in that case.
it isn't any kind of special handling, it's just, if it returns a promise, now it has to wait for that promise to resolve to know what gets passed along in the chain.
so my above assumption is fine right
i just don't like the wording, i guess. you aren't registering a callback multiple times
it's just one
the first .then doesn't care about what happens within the new promise
it returns that promise, and then the chain continues on once that promise resolves
@KevinB Yes it doesn't care.. But it actually creates a new promise to be returned to next .then() in the chain
20:24
@KevinB If it won't register the second time (due to return new Promise) then how will the handler be called?
you returned the promise
so the promise created by .then won't be resolved until that promise is resolved
@KevinB So that promise will only get resolved not registered?
i'm honestly not sure what registered is referring to
here's your code broken down a bit more
var x = (v) => new Promise( resolve => resolve(v * 2) );
var a = Promise.resolve( 21 );
var b = a.then( function(v){
  console.log( v ); // 21
  return x(v);
});
var c = b.then(function(v){
  console.log(v); // 42
});
it functions identically, except that there's an additional function, x, that returns a promise
Promises are like listeners .. so that when it gets resolved, the associated aka registered handler will be called. So the registering is must for the handler to be called
the callback you passed to b's .then doesn't care what x does
it only returns the result
it doesn't "register" a callback
if that result happens to be a promise, then the promise represented by b won't be resolved until that promise is resolved
you can even create the promise ahead of time for the same result
the act of using .then isn't what defines what the inner promise does
I don't like the event listener way of looking at it, but that's a fair comparison i think
20:33
@KevinB hmm .. its relatable
but, effectively, the promise contained in b above is produced immediately, upon calling a.then()
the callback you give .then is called when a resolves and returns a new promise. now you're waiting for that promise to resolve before b can resolve
b didn't define a callback, the function x did. that promise could have even already existed prior to a resolving
my point is it's a separate part of the process
@KevinB YEs i agree with that from the beginning .. But im stuck about the promises 'b' holds
b holds a new promise that is bound to the result of the promise x() returns
so the promise returned from a.then() is overwritten? due to new promise
or shadowed ... something like that?
no, it's... different
kinda like how {} and {} are different. Yes both b and the the promise returned by x will resolve at the same time, but they aren't the same promise
20:42
@KevinB Yes
var x = (v) => new Promise( resolve => resolve(v * 2) );
var y;
var a = Promise.resolve( 21 );
var b = a.then( function(v){
  console.log( v ); // 21
  y = x(v);
  return y;
});
var c = b.then(function(v){
  console.log(v); // 42
  console.log(y === b); // false
});
that's why 4 promises in this code
y here is set to the promise returned by x(v), and can independantly have it's own .then called on it without affecting b
@KevinB Exactly!
The whole point of the promise system is for each step of the way to be independently accessible/usable
if by returning a promise to .then the .then became that promise, that would be lost
so instead, it just resolves the original promise with the result
20:46
var b = a.then( function(v){
  console.log( v ); // 21
  y = x(v);
  return y;
});
This part will get 2 promises, first from a.then(). Later, from y
right. a.then( creates a promise, and x(v) creates one
so by that i was saying ... that both promises are associated with the same handler
you could then do b.then( v => y.then( v => x(v) ) ).then(console.log) to get 84, without interfereing with c
posted on October 02, 2023 by Matt

The Stable channel is being updated to OS version: 15572.57.0 Browser version: 117.0.5938.144 for most ChromeOS devices. If you find new issues, please let us know one of the following ways File a bugVisit our ChromeOS communitiesGeneral: Chromebook Help CommunityBeta Specific: ChromeOS Beta Help CommunityReport an issue or send feedback on ChromeInterested in swit

because y is independant of b's outcome
20:52
@KevinB yes
err, that'd be clearer if i replace v in b.then( v with _
since it is unused, ;)
the purpose of the b.then there is just to ensure y has been defined
21:20
hi
@Michelle Welcome to the JavaScript chat! Please review the room rules. If you have a question, just post it, and if anyone's free and interested they'll help. If you want to report an abusive user or a problem in this room, visit our meta.
I need to implement an infinite scrolling, I have a loadmoremessage function but it's not working. Please can anybody help?
why isn't it working?
I'm using React native, here I explain in more detail what's happening stackoverflow.com/questions/77199375/…
const loadMoreMessages = async () => {
    const getEarlyMessage = last(sortedMessages);
    if (isEmpty(room)) return;

    const options = { roomId: room?.id, limit: 10, _id: getEarlyMessage._id };
    const data = await getRoomMessages(options);
    dispatch(addMessage(data));
};
so, first, is the room being reported as empty?
effectively, work through the function until you find which piece of it isn't working
there's several bits there that could be returning something unexpected and causing it to not do anything
21:27
ok I'll do that now
 
1 hour later…
22:57
@VLAZ I hope you are joking xD We've both seen worse from high-reps precisely because it doesn't measure knowledge (oh, how I wish more than 1 person at SE understood that)

« first day (4734 days earlier)      last day (439 days later) »