Exploring the world of TypeScript, I recently attempted to convert some basic JavaScript promise samples into TypeScript promises. While working on this conversion process, I encountered an issue that has left me puzzled and unable to find a solution even after searching online.
Situation:
I have a function that returns a Promise
which resolves to a number
upon execution. Additionally, I wanted to test various then
scenarios with this sample as well.
Below is the code snippet for reference:
function test_promise(): Promise<number>{
return new Promise((resolve, reject) :number => {
let result:number = 10;
resolve(result);
}).then(result => { // first then
console.log("Result: " + (typeof result)); // Result: number
return result * 2; //
}).then(result => { // second then
return result * 2;
}).then(result => { // third then
return result * 2;
});
}
I have included two screenshots below for better understanding.
Screenshot 1:
https://i.sstatic.net/S795t.png
Screenshot 2
https://i.sstatic.net/VZK2v.png
Currently, there are a couple of aspects that remain unclear to me:
- In screenshot 1, the hint item does not indicate that the
typeof
result isnumber
, yet when printed usingconsole.log
, it shows as anumber
. What could be causing this discrepancy? - If the console displays the type as
number
, why does it prevent me from performing multiplication operations on it?
What modifications should I make in order to get this sample functioning correctly?
I would appreciate any insights or guidance you can provide regarding this issue. Thank you.
Best regards,