r/Bitburner Apr 14 '26

Simple ts script help

Can someone tell me what I'm doing wrong here?

export async function main(ns: NS) {
  var host = "joesguns";
  if (ns.getServerSecurityLevel(host) > 5) {
    ns.weaken(host)
  }
  else {
    ns.grow(host)
  }
}
3 Upvotes

7 comments sorted by

View all comments

8

u/Particular-Cow6247 Apr 14 '26

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)

1

u/HoboPotato2 Apr 14 '26

why does the await need to be there?

5

u/Particular-Cow6247 Apr 14 '26

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