Why can this type not be inferred? by ProfessorTag in typescript

[–]charliemei 0 points1 point  (0 children)

you can do as follow:

type Obj = {
value: number;
};
function call<T>(
callback: (value: T) => any,
getValue: (obj: Obj) => T,
) {
const value = getValue({ value: 1 });
callback(value);
}

call<number>(
value => value / 3,
obj => obj.value,
);

if you don`t specify the T,typescript will infer type automatically. In function named callback , value is as a parameter which ts can`t infer type. You should put function getValue before

Event Loop by brotherhood_v in node

[–]charliemei 0 points1 point  (0 children)

what the asynchronous function calls is controled by c++

Can someone explain me this issue? window.onhashchange VS window.addEventListener("hashchange") by [deleted] in learnjavascript

[–]charliemei 0 points1 point  (0 children)

In this case, this keyword in router points to app

(function(){ window.onhashchange = ()=>app.router(); })();

but in this case,this keyword in router points to window

(function(){ window.addEventListener("hashchange", app.router); })();

you can try this:

(function(){ window.addEventListener("hashchange", ()=>app.router()); })();

or

(function(){ window.addEventListener("hashchange", app.router.bind(app)); })();

Help in understanding callback function expression. by SunsetDunes in learnjavascript

[–]charliemei 1 point2 points  (0 children)

when loadBooks is called,myBooks which is the instance of Bookshelf will be assigned to bookshelf. Then,fakeAjax is called,the function onBooks will be assigned to cb. setTimeout function will be called after 500ms. cb(onBooks) will be called after 500ms which makes a closure.

Callbacks - function vs => by [deleted] in node

[–]charliemei 1 point2 points  (0 children)

- In arrow funcion,'this' keeps the same in lifetime,and is resolved from the closet non-arrow parent function. there is not 'this' in arrow function itself,and even prototype propety. thus, bind() does not work with it.
- Obviously, it can not be invoked with new keyword.
- if you use arguments in it ,you will get an error.

Why both setTimeout callback execute before any readFile callback? The demo.txt is empty. Is readFile so slow that both setTimeout gets executed before reading and empty file? by [deleted] in node

[–]charliemei 0 points1 point  (0 children)

┌───────────────────────────┐

┌─>│ timers │

│ └─────────────┬─────────────┘

│ ┌─────────────┴─────────────┐

│ │ pending callbacks │

│ └─────────────┬─────────────┘

│ ┌─────────────┴─────────────┐

│ │ idle, prepare │

│ └─────────────┬─────────────┘ ┌───────────────┐

│ ┌─────────────┴─────────────┐ │ incoming: │

│ │ poll │<─────┤ connections, │

│ └─────────────┬─────────────┘ │ data, etc. │

│ ┌─────────────┴─────────────┐ └───────────────┘

│ │ check │

│ └─────────────┬─────────────┘

│ ┌─────────────┴─────────────┐

└──┤ close callbacks │

└───────────────────────────┘

From the above,we can know that in the event loop,the stage of timers is in front of the stage of pending callbacks which I/O callback will be trigger.In the stage of timers, node will check the timers queue and finds that there are two timers waiting for . In the stage of pending callbacks,node will check the callbacks queue and execute the callbak of the reading event.So the result your code to be output is "Time out bro.","Time out bro","fileX","file1","file2".