Check out a free preview of the full State Machines in JavaScript with XState course:
The "The Actor Model" Lesson is part of the full, State Machines in JavaScript with XState course featured in this preview video. Here's what you'd learn in this lesson:

David explains that the actor model is an entity that can send a message to another actor, can create new actors, or can change its behavior as a response to a message. An actor is embodied by using invoke, and can invoke a promise, a callback, an observable, or a machine.

Get Unlimited Access Now

Transcript from the "The Actor Model" Lesson

[00:00:00]
>> Now the actor model is not directly related to state machines and state charts. But the actor model works really well with state machines and state charts. If you're unfamiliar with the actor model and you look online, just like state machines and state charts, it could seem like a really alienating complicated thing.

[00:00:18] But the actor model in essence is really simple, because you could think of pretty much everything around you as an actor. In fact, I'm an actor, not because I'm in front of a camera or anything. You're an actor, devices that communicate with each other are each actors. So what is an actor?

[00:00:37] An actor is a thing or an entity that could do one of three things, an actor can send a message to another actor, it could create new actors, and so basically have child actors or it could change its behavior in response to a message. So for instance, let's say that we have this machine, which is a fetcher machine.

[00:01:04] And its job is to call some sort of promise or something that we could consider an actor. So this is doing the second thing over here, which is spawning a new actor. So in this spectrum machine, we are invoking a new actor. And so this actor is going to do whatever it wants to do.

[00:01:24] We don't really know what's inside an actor. We can't read it states and this is on purpose. Actor state is private and local. And this makes it really, really useful for distributed systems and things like that. So an actor state is local, but the actor can eventually send a message back to the parents.

[00:01:45] And so if you think about a promise as an actor, it's exactly the same way. You invoke a promise, so you're calling it, it's doing something in the background, maybe calling an API, maybe doing some sort of process, we don't know and we don't care until the promise resolves.

[00:02:01] And when it resolves, with promises we have dot then and we have a callback. And so that's our way of saying, when you're done, send us back a message using this callback. And so actors could of course call other actors, which could call other actors as well. And so the actor model in x state, one of the ways it's embodied, is using invoke.

[00:02:27] And so invoke allows you to invoke a promise, a callback, an observable or even another machine with the understanding that those things can send messages back to the original machine. So we're gonna be doing this exercise together, so that I could demonstrate to you exactly how actors work.

[00:02:49] And just getting around the mental model of what an actor is.