mirror of https://github.com/sveltejs/svelte
				
				
				
			Merge pull request #1418 from sveltejs/await-helper
	
		
	
				
					
				
			Handle await block promises in a shared helperpull/1422/head
						commit
						49c594e526
					
				@ -0,0 +1,46 @@
 | 
				
			|||||||
 | 
					import { assign, isPromise } from './utils.js';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function handlePromise(promise, info) {
 | 
				
			||||||
 | 
						var token = info.token = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						function update(type, key, value) {
 | 
				
			||||||
 | 
							if (info.token !== token) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							info.resolved = key && { [key]: value };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							const child_ctx = assign(assign({}, info.ctx), info.resolved);
 | 
				
			||||||
 | 
							const block = type && (info.current = type)(info.component, child_ctx);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (info.block) {
 | 
				
			||||||
 | 
								info.block.u();
 | 
				
			||||||
 | 
								info.block.d();
 | 
				
			||||||
 | 
								block.c();
 | 
				
			||||||
 | 
								block.m(info.mount(), info.anchor);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								info.component.root.set({});
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							info.block = block;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (isPromise(promise)) {
 | 
				
			||||||
 | 
							promise.then(value => {
 | 
				
			||||||
 | 
								update(info.then, info.value, value);
 | 
				
			||||||
 | 
							}, error => {
 | 
				
			||||||
 | 
								update(info.catch, info.error, error);
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// if we previously had a then/catch block, destroy it
 | 
				
			||||||
 | 
							if (info.current !== info.pending) {
 | 
				
			||||||
 | 
								update(info.pending);
 | 
				
			||||||
 | 
								return true;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							if (info.current !== info.then) {
 | 
				
			||||||
 | 
								update(info.then, info.value, promise);
 | 
				
			||||||
 | 
								return true;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							info.resolved = { [info.value]: promise };
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,47 @@
 | 
				
			|||||||
 | 
					let fulfil;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					let promise = new Promise(f => {
 | 
				
			||||||
 | 
						fulfil = f;
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default {
 | 
				
			||||||
 | 
						data: {
 | 
				
			||||||
 | 
							promise
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						html: `
 | 
				
			||||||
 | 
							<p>loading...</p>
 | 
				
			||||||
 | 
						`,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						test(assert, component, target) {
 | 
				
			||||||
 | 
							fulfil(42);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return promise
 | 
				
			||||||
 | 
								.then(() => {
 | 
				
			||||||
 | 
									assert.htmlEqual(target.innerHTML, `
 | 
				
			||||||
 | 
										<p>loaded</p>
 | 
				
			||||||
 | 
									`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									promise = new Promise((f, r) => {
 | 
				
			||||||
 | 
										fulfil = f;
 | 
				
			||||||
 | 
									});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									component.set({
 | 
				
			||||||
 | 
										promise
 | 
				
			||||||
 | 
									});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									assert.htmlEqual(target.innerHTML, `
 | 
				
			||||||
 | 
										<p>loading...</p>
 | 
				
			||||||
 | 
									`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									fulfil(43);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									return promise.then(() => {});
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								.then(() => {
 | 
				
			||||||
 | 
									assert.htmlEqual(target.innerHTML, `
 | 
				
			||||||
 | 
										<p>loaded</p>
 | 
				
			||||||
 | 
									`);
 | 
				
			||||||
 | 
								});
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					{#await promise}
 | 
				
			||||||
 | 
						<p>loading...</p>
 | 
				
			||||||
 | 
					{:then value}
 | 
				
			||||||
 | 
						<p>loaded</p>
 | 
				
			||||||
 | 
					{:catch error}
 | 
				
			||||||
 | 
						<p>errored</p>
 | 
				
			||||||
 | 
					{/await}
 | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue