Transcript from the "Exit Codes" Lesson
>> Kyle Simpson: Okay, but notice that it's returning us back a 0. That is the default exit code when a process finishes with success, okay? So let's just go back to our child for a moment. And before we finish, let's set our exit code, process.exitCode = 1.
>> Kyle Simpson: And then if we run our ex7 again,
>> Kyle Simpson: You notice now that exit code is coming back, it's detecting that that exit code was 1. In POSIX standard convention, a 0 exit code means everything was fine, anything non-zero means something went wrong, okay? And we're going to use the exit code as our communication channel, instead of worrying about standard in and standard out.
[00:00:56] We're just going to use exit code that if our child is successful at its task, it's going to return 0, and if it's unsuccessful for any reason, it's going to return one. And so our parent, our ex7, is going to need to listen for these children and see if their exit code is returning back 0 or not to know whether it was successful.
[00:01:20] So ex7-child, what we really want it to do is not do a for loop. What we really want it to do is make an AJAX request. We're going to use fetch and we're going to fetch against our server. So let's call fetch, and here since we're not in a browser we are going to have to give it a full URL to use.
[00:01:49] So we're going to say http://localhost:8039, or whatever port you chose, and then /get-records. And we're going to say, res = await fetch,
>> Kyle Simpson: If, let's do this. Let's do a try catch because these can fail if things are down.
>> Kyle Simpson: So then if res && res.ok, then we want to say,
>> Kyle Simpson: records = await res.json. Same thing we did in the web browser. We're basically just now doing it on the server. I think it was asked earlier in the course how would I handle making outbound requests. This is exactly how I would do it. I'd use fetch exactly like I do in a browser.
[00:03:03] I'd just use it here on node.
>> Kyle Simpson: Okay, so if (records && records.length > 0) then we know we were successful. So let's just for good measure, set process.exitCode = 0, and return. And otherwise, if any of that fails for any reason, then we want to set exitCode = 1.
>> Kyle Simpson: Let's try our ex7-child right now. And it just runs for a little bit, and it doesn't give us any indication about whether it succeeded or not. So how could we test from the command line what the exit code is? We can't really see the exit code, but there is a way to implicitly determine what that exit code might have done.
[00:03:57] So the way we're going to implicitly determine that is to observe that the reason for exit codes is because the way the command line works if you're doing multiple commands, is that if any command fails and fives a non-zero exit code, it stops the rest of the processing from happening.
[00:04:14] So for example, if I were to say node ex7-child.js && and then do a directory listing. The directory listing is only going to happen if ex7-child.js gives us a 0 exit code. So when we run it, we noticed that we got a directory listing, so that means it's successful.
[00:04:36] I'm going to run that same command. But before I run that command, I'm going to take the server down. And now when I run the command, it fails right away because we got an exit code of 1, which then caused it to short circuit and not run. So that's how you can test for yourself that in fact you've got a child process doing what it's supposed to be doing, giving you a 1 or a 0.