Home / Function/ await_block() — svelte Function Reference

await_block() — svelte Function Reference

Architecture documentation for the await_block() function in await.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  6b775363_a430_a650_b5bb_43f6c298e1fe["await_block()"]
  277e61a1_318f_c1a6_cf40_f6e1a0f3aa2f["await.js"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|defined in| 277e61a1_318f_c1a6_cf40_f6e1a0f3aa2f
  b31601aa_35ce_7827_5394_99fb97fa27d2["hydrate_next()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| b31601aa_35ce_7827_5394_99fb97fa27d2
  ad360ec3_c98b_ac47_212b_923d0363a0ce["is_runes()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| ad360ec3_c98b_ac47_212b_923d0363a0ce
  1e2f7428_6050_5cb7_69db_bf5db719f6d1["source()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| 1e2f7428_6050_5cb7_69db_bf5db719f6d1
  03788141_01d2_5299_6e22_4211e661afe4["mutable_source()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| 03788141_01d2_5299_6e22_4211e661afe4
  1bd7dd6f_4c22_6f44_9747_fc5ea0deaa7b["block()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| 1bd7dd6f_4c22_6f44_9747_fc5ea0deaa7b
  b898b311_bec4_34ca_1aa6_2a0ff69b0212["is_promise()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| b898b311_bec4_34ca_1aa6_2a0ff69b0212
  40f27ad3_30bb_8f2a_3fb3_757088cf7428["set_hydrate_node()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| 40f27ad3_30bb_8f2a_3fb3_757088cf7428
  8bcc1a1c_73ab_4fe7_59be_b28bbe88fd3e["skip_nodes()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| 8bcc1a1c_73ab_4fe7_59be_b28bbe88fd3e
  f5b61c69_d41c_bdb7_b931_5b8b3374332c["set_hydrating()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| f5b61c69_d41c_bdb7_b931_5b8b3374332c
  df71411d_ad64_65b8_da29_e77890377349["unset_context()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| df71411d_ad64_65b8_da29_e77890377349
  5bc3c950_96f7_e454_6cb7_65ffc2179811["flushSync()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| 5bc3c950_96f7_e454_6cb7_65ffc2179811
  fc566ca0_2101_ea1a_cf42_44d2442cc526["internal_set()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| fc566ca0_2101_ea1a_cf42_44d2442cc526
  2cab0f64_6d19_d981_66e2_d2555c252702["queue_micro_task()"]
  6b775363_a430_a650_b5bb_43f6c298e1fe -->|calls| 2cab0f64_6d19_d981_66e2_d2555c252702
  style 6b775363_a430_a650_b5bb_43f6c298e1fe fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/internal/client/dom/blocks/await.js lines 34–142

export function await_block(node, get_input, pending_fn, then_fn, catch_fn) {
	if (hydrating) {
		hydrate_next();
	}

	var runes = is_runes();

	var v = /** @type {V} */ (UNINITIALIZED);
	var value = runes ? source(v) : mutable_source(v, false, false);
	var error = runes ? source(v) : mutable_source(v, false, false);

	var branches = new BranchManager(node);

	block(() => {
		var input = get_input();
		var destroyed = false;

		/** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
		// @ts-ignore coercing `node` to a `Comment` causes TypeScript and Prettier to fight
		let mismatch = hydrating && is_promise(input) === (node.data === HYDRATION_START_ELSE);

		if (mismatch) {
			// Hydration mismatch: remove everything inside the anchor and start fresh
			set_hydrate_node(skip_nodes());
			set_hydrating(false);
		}

		if (is_promise(input)) {
			var restore = capture();
			var resolved = false;

			/**
			 * @param {() => void} fn
			 */
			const resolve = (fn) => {
				if (destroyed) return;

				resolved = true;
				// We don't want to restore the previous batch here; {#await} blocks don't follow the async logic
				// we have elsewhere, instead pending/resolve/fail states are each their own batch so to speak.
				restore(false);
				// Make sure we have a batch, since the branch manager expects one to exist
				Batch.ensure();

				if (hydrating) {
					// `restore()` could set `hydrating` to `true`, which we very much
					// don't want — we want to restore everything _except_ this
					set_hydrating(false);
				}

				try {
					fn();
				} finally {
					unset_context();

					// without this, the DOM does not update until two ticks after the promise
					// resolves, which is unexpected behaviour (and somewhat irksome to test)
					if (!is_flushing_sync) flushSync();
				}
			};

			input.then(
				(v) => {
					resolve(() => {
						internal_set(value, v);
						branches.ensure(THEN, then_fn && ((target) => then_fn(target, value)));
					});
				},
				(e) => {
					resolve(() => {
						internal_set(error, e);
						branches.ensure(THEN, catch_fn && ((target) => catch_fn(target, error)));

						if (!catch_fn) {
							// Rethrow the error if no catch block exists
							throw error.v;
						}
					});
				}
			);

Domain

Subdomains

Frequently Asked Questions

What does await_block() do?
await_block() is a function in the svelte codebase, defined in packages/svelte/src/internal/client/dom/blocks/await.js.
Where is await_block() defined?
await_block() is defined in packages/svelte/src/internal/client/dom/blocks/await.js at line 34.
What does await_block() call?
await_block() calls 15 function(s): block, capture, ensure, flushSync, hydrate_next, internal_set, is_promise, is_runes, and 7 more.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free