Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Getting 'DataCloneError' if function returns Promise #63

Open
omkar-kanekar1-tfs opened this issue Dec 6, 2023 · 3 comments
Open

Getting 'DataCloneError' if function returns Promise #63

omkar-kanekar1-tfs opened this issue Dec 6, 2023 · 3 comments

Comments

@omkar-kanekar1-tfs
Copy link

Describe the bug
Host exposed a function which returns "Promise". On calling that function from iframe url code, we get following error "Response object could not be cloned." If the Host function returns objects/array/string it works fine.
What is the workaround.

@alesgenova
Copy link
Owner

Functions returning promises are supported, and that's probably not where the issue is.
What type of parameters does the function take? And what is the type resolved by the Promise?

@omkar-kanekar1-tfs
Copy link
Author

The function takes string and and object as input The promise returns fetch "Response" object. Basically we are invoking overridden version of JavaScript fetch and the same is retuned from function.

Reference:
function f(url, fetchOpt) {
return customFetch(url, fetchOpt)
}

@alesgenova
Copy link
Owner

My guess is that the Response object can't be passed across windows or workers.
See which type of objects can be natively be passed across (either as function parameters, or returned): https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm

So a work around for your use case could be to return the content of the response body instead of the Response itself.

Double check the code, but it could look something like this:

async function f(url, options) {
  let response = await fetch(url, options);
  let body = await response.json();
  return body;
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants