Skip to content

Commit

Permalink
[fix] Improve is_promise handling (#8162)
Browse files Browse the repository at this point in the history
* correctly handle promises that are of function type

* add License

Co-authored-by: Vilsol <[email protected]>
  • Loading branch information
baseballyama and Vilsol authored Jan 3, 2023
1 parent e1a1c7f commit 4b84c4d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/runtime/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export function assign<T, S>(tar: T, src: S): T & S {
return tar as T & S;
}

// Adapted from https://github.com/then/is-promise/blob/master/index.js
// Distributed under MIT License https://github.com/then/is-promise/blob/master/LICENSE
export function is_promise<T = any>(value: any): value is PromiseLike<T> {
return value && typeof value === 'object' && typeof value.then === 'function';
return !!value && (typeof value === 'object' || typeof value === 'function') && typeof value.then === 'function';
}

export function add_location(element, file, line, column, char) {
Expand Down
19 changes: 19 additions & 0 deletions test/runtime/samples/await-function-promise/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const realPromise = Promise.resolve(42);

const promise = () => {};
promise.then = realPromise.then.bind(realPromise);
promise.catch = realPromise.catch.bind(realPromise);

export default {
props: {
promise
},

test({ assert, target }) {
return promise.then(() => {
assert.htmlEqual(target.innerHTML, `
<p>42</p>
`);
});
}
};
7 changes: 7 additions & 0 deletions test/runtime/samples/await-function-promise/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export let promise;
</script>

{#await promise then value}
<p>{JSON.stringify(value)}</p>
{/await}

0 comments on commit 4b84c4d

Please sign in to comment.