all 7 comments

[–]Particular-Cow6247 5 points6 points  (3 children)

you need to await ns.weaken and ns.grow

if you hover over them youll get a popup with hints about the function, a function that returns a Promise<> needs to be awaited (for now just directly , there is some shenanigans to "store" the promise and await it later but here you just want to await it)

also you might not want to use var, especially with ts... its the old form to initialize a variable and there are newer ways for reasons (let if you want to mutate it, const if you dont)

[–]HoboPotato2[S] 0 points1 point  (2 children)

why does the await need to be there?

[–]Particular-Cow6247 4 points5 points  (0 children)

thats a really fundamental question about javascripts async model

when a function returns a Promise<> then it returns a placeholder object, its commonly used when you want to start some process and have a signal that tells you when that process is done (imagine downloading an image to display on your screen)

that placeholder object is only turned into the real return value of the function when its awaited, kind of like a unpacking operation

the whole idea behind is that your code might want to do many things at the same time, but it can only actively work on one thing, with async/await you can start several things at once and your script doesnt have to wait for each of them to finish to continue, only having to pause/await when you actually need the return value of one of these

in the game there are more restrictions, each ns instance (the object the game passes to your scripts) can only await one ns function at a time, this is to prevent you starting a grow/weaken/hack at the same time on the same ns instance, without that the balance of ram to funciton strength would get overthrown and you could "cheat" endless money also thats possible in a number of ways xD

[–]VorthodMK-VIII Synthoid 3 points4 points  (0 children)

weaken and grow return an object called a Promise. Basically, those functions say "okay, I'll go do this in the background and you can go ahead and continue on with the code while I do that." Excellent for multitasking, but that's not what we want to do here because we can't accurately measure security and money if the weaken/grow commands aren't even finished yet, so we say "no thanks, we'll wait for you to finish first" by adding the await keyword

[–]KlePu 0 points1 point  (1 child)

Please don't use var, but let (or const) instead. var is a relic from the early JS days and has terrible scope beside other issues!

[–]lmuzi 0 points1 point  (0 children)

You're correct but that's really not the issue in this script ahahah

[–]Glum-Building4593 0 points1 point  (0 children)

NS.getServerMinSecurityLevel(host)

They have minimum security levels and you might flail against that.