Repository navigation
fix(blaze): await async bindings when expanding views in HTML attributes - #537
Merged
nachocodoner merged 4 commits intoNov 12, 2026
Merged
Conversation
dupontbertrand
marked this pull request as draft
November 11, 2026 01:48
dupontbertrand
force-pushed
the
fix/async-expand-view
branch
from
November 11, 2026 10:44
d7dd626 to
4c25021
Compare
dupontbertrand
marked this pull request as ready for review
November 11, 2026 10:51
… bindings Scope the waiting for async bindings to the expansion driven by the attribute updater of the materializer, the only caller that can await the settled values (meteor#470). `Blaze.toHTML`, `Blaze.toHTMLWithData` and `Blaze._toText` render synchronously as before, with a pending binding rendered as an empty block, instead of leaving a `Promise` in the htmljs that `HTML.toHTML` rejects with "Unexpected object in htmljs". A binding re-run invalidates the updater only when it returned a `Promise`; a synchronous binding keeps re-expanding only when its value changes, as in 3.0. Tests: a binding that settles after a macrotask, and `Blaze.toHTML` with a pending `#if`, `#each` and attribute block.
…k once With the attribute views awaiting their bindings, an error thrown while the resolved data is read, for example by a getter, no longer surfaces through the updater computation: it rejects the attribute promise chain. Cover that path with the single-report tests of the attribute chain, so the chain reports it once. This needs the fix of the last `then()` from the previous commits.
dupontbertrand
force-pushed
the
fix/async-expand-view
branch
from
November 11, 2026 10:54
4c25021 to
99411a7
Compare
…hile pending A run of the attribute updater whose attribute views await async bindings settles later. When a binding re-ran meanwhile, the updater was invalidated and re-run, but the older run's chain still reached `attrUpdater.update()` once its promises settled, writing the result of a destroyed view over the newer one (review of meteor#537). The run records its own invalidation with `Tracker.onInvalidate` and skips the write. Regression: two overlapping updates whose promises settle in reverse order, and a synchronous value replacing a pending promise.
nachocodoner
approved these changes
Nov 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #470.
When a built-in block helper (
#if,#unless,#with,#each) inside an HTML attribute (e.g.<div class="base {{#if myProp}}active{{/if}}">) evaluated an async helper or an async template argument ({{> Child myProp=asyncHelper}}),Blaze._expandViewranview._render()synchronously before the binding'sPromisesettled.Because
view._render()readundefinedfrom the binding'sReactiveVarinside the attribute updater's computation (updaterComputation) andBlaze._expandViewregisteredTracker.onInvalidate(() => Blaze._destroyView(view)), thePromisesettling on the next microtask invalidatedupdaterComputation, immediately destroying the subview and creating a fresh one with an unresolved binding on every flush (leaving the attribute stuck and re-running the helper in an infinite loop).Change
Background. Blaze renders a view in two ways. The usual way materializes it: the view gets a render computation of its own, so a helper that returns a
Promisecan update the DOM once thePromisesettles. The other way expands it to plain htmljs, with no computation of its own: that is whatBlaze.toHTMLdoes, and what the attribute updater does for an attribute value that contains a block helper, likeclass="base {{#if myProp}}active{{/if}}". The attribute updater is an autorun (updaterComputationinmaterializer.js): on every run it expands the views of the attribute and writes the resulting strings on the element.The bug. When the
#ifinside the attribute got aPromise, the expanded view rendered at once with its binding still pending, so the attribute was computed fromundefined. When thePromisesettled, the binding'sReactiveVarchanged and invalidated the updater. The updater destroys and re-creates its expanded views on every run, so the new#ifcalled the helper again, got a new pendingPromise, and the cycle started over: the attribute never received its value and the helper ran forever.The fix, step by step.
The updater asks for the wait.
materializer.jsnow callsBlaze._expandAttributes(rawAttrs, parentView, true /*awaitBindings*/). The flag travels with the expander (Blaze._HTMLJSExpander) down to every view found in the attribute, nested ones included ({{#if a}}{{#if b}}…), throughBlaze._expandandBlaze._expandView.The view collects its pending bindings. When a view is expanded with the flag,
Blaze._expandViewmarks it (view._awaitBindings) and remembers the updater's computation (view._expandComputation) before thecreatedcallbacks run. In those callbacks,_createBinding(behind#if,#unless,#with,#let,#eachand template arguments) stores onview._bindingPromisesthePromiseof each binding that returned one._setBindingValuenow returns thatPromise, which settles once the value is written in theReactiveVar.The render waits for them. If the view has pending bindings,
Blaze._expandViewreturns aPromise: it waits for all of them, then renders insideTracker.withComputation(updaterComputation, …), so the reactive reads made during the render still register as dependencies of the updater, exactly as a synchronous render would. The promises left in the result by nested views are unwrapped (waitForExpanded) before the result reacheswaitForAllAttributes, which already knew how to await attribute values.A later change re-expands instead of re-rendering. An expanded view cannot re-render itself. So when a binding's autorun re-runs and returns a new
Promise, it invalidates the updater, which drops the view and expands a fresh one that waits again. A binding that returns a plain value needs nothing special: the updater already depends on itsReactiveVarand re-runs only when the value changes, as in Blaze 3.0.#eachwaits before observing.Blaze.Eachsets upObserveSequence.observeonly once its sequence binding has settled, so the items (or the{{else}}block) come from the real sequence, not fromundefined.A replaced run does not write. A run of the updater can be invalidated while its attribute
Promises are still pending: its bindings re-ran, or the element was torn down. The updater records that withTracker.onInvalidateinside the run and skips the DOM write when the result settles, so a slowerPromisefrom an earlier run never overwrites the result of the run that replaced it.What does not change. Without the flag,
Blaze._expandViewbehaves as before:Blaze.toHTML,Blaze.toHTMLWithDataandBlaze._toTextrender synchronously, a pending binding renders as an empty block, and the result is never aPromise. The materialized path (Blaze.render) is not touched: its views were never expanded.Tests.
packages/spacebars-tests/async_tests.{html,js}: 18 tests covering#if,#unless,#let,#with,#each..in, template inclusion with an async keyword argument ({{> child x=asyncHelper}}), a reactive update without an infinite loop, a binding that settles after a macrotask, two overlapping updates whose promises settle in reverse order (and a synchronous value replacing a pending promise), andBlaze.toHTMLwith a pending#if,#eachand attribute block.Verification
Full headless Tinytest suite in
test-app, without jQuery (native DOM backend) and with--extra-packages=jquery:461 / 0 / 0 / 461passed465 / 0 / 0 / 465passedThe three
Blaze.toHTMLtests fail on the previous commit of this branch withUnexpected object in htmljs in toHTML: [object Promise], and pass with the scoped waiting.Changelog
blaze: Fix async block helpers (#if,#unless,#with,#each) and async template parameters inside HTML attributes (Bad resolve with template async parameter #470).