Advanced 30 min read

Imperative Calls vs the Wire Service (the Apex Side)

By the end of this lesson, you'll be able to:

  • Explain the difference between an imperative call and a wired method, from the Apex side
  • Recognize which @AuraEnabled shape fits each calling style
  • Understand why this distinction matters even without writing the LWC JavaScript itself

Prerequisites: "@AuraEnabled Methods"

The same @AuraEnabled method serves both styles

@AuraEnabled(cacheable=true)
public static List<Account> getAccounts() {
    return [SELECT Id, Name FROM Account];
}

From the Apex side, both calling styles use the exact same method — the difference is entirely in how the LWC's JavaScript invokes it. An imperative call is like calling any regular function: "call this now, and give me the result." The wire service instead subscribes to the method's output as a reactive data stream, automatically re-calling it when relevant data (like a record Id passed as a parameter) changes.

Why this matters even without writing the JavaScript

The wire service requires cacheable=true on @AuraEnabled (covered fully in the next lesson) — an imperative call doesn't require this at all. This means the Apex method's design needs to anticipate which calling style it's meant for, even though the actual JavaScript that decides "call this imperatively" or "wire this" is written entirely on the LWC side, outside this course's scope.

A practical rule of thumb

A method meant to be read reactively — data that should refresh automatically as the UI's state changes, like "the current record's related Opportunities" — is typically designed for the wire service, and needs cacheable=true. A method meant to do something — save a record, run a calculation on demand, trigger an action — is typically called imperatively, and should generally not be marked cacheable=true, since it performs an action rather than simply reading data.

Exercise

As a comment, decide whether a method that saves a new Opportunity record should be designed for imperative calls or the wire service, and why.

Show hint

Think about whether the method performs an action or just reads data.

APEX

Imperative Calls vs the Wire Service (the Apex Side) Quiz

1. What does the wire service do that an imperative call does not?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

An LWC can call Apex in two different styles — imperatively (like a regular function call) or via the wire service (a reactive data stream) — and while the calling code lives in JavaScript, the Apex method itself needs to be written with the right shape for each.