[fix] Improve `is_promise` handling (#8162)

* correctly handle promises that are of function type

* add License

Co-authored-by: Vilsol <me@vil.so>
pull/8175/head
Yuichiro Yamashita 2 years ago committed by GitHub
parent e1a1c7fa87
commit 4b84c4df3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,8 +10,10 @@ export function assign<T, S>(tar: T, src: S): T & S {
return tar as 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> { 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) { export function add_location(element, file, line, column, char) {

@ -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>
`);
});
}
};

@ -0,0 +1,7 @@
<script>
export let promise;
</script>
{#await promise then value}
<p>{JSON.stringify(value)}</p>
{/await}
Loading…
Cancel
Save