Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Deferred.resolve not passing data... Works in chrome. #95

Closed
PCSmith opened this issue Mar 3, 2018 · 5 comments
Closed

Deferred.resolve not passing data... Works in chrome. #95

PCSmith opened this issue Mar 3, 2018 · 5 comments

Comments

@PCSmith
Copy link

PCSmith commented Mar 3, 2018

Extension pop up window sends message requesting important info:

let Params = { Event: 'CreateNewPostInit' };

browser.runtime.sendMessage(Params).then(function (response) {
	console.log(response.ImportantInfo);
});

Background page receives:

browser.runtime.onMessage.addListener(GotMessage);

function GotMessage(request, sender) {
	var defd = null;

	switch (request.Event) {
		case "CreateNewPostInit":
			defd = $.Deferred();
			CreateNewPostInit(request, sender, defd);
			return defd.promise();
			break;
	}
}

function CreateNewPostInit(request, sender, defd) {
	defd.resolve({ ImportantInfo: "42" });
}

In Chrome "42" is logged to the console from the line in the then after the sendMessage. Firefox throws a "TypeError: response is undefined".

Thanks for the assistance in advance.

@Rob--W
Copy link
Member

Rob--W commented Mar 5, 2018

Firefox expects a real promise, from the same scope of the script (i.e. Promise created in a different execution context such as a child frame or popup won't work), whereas the polyfill accepts any thenable (object with a then method).

Don't use jQuery's Deferred, use native promises instead.

@PCSmith
Copy link
Author

PCSmith commented Mar 5, 2018

Thanks for the info -- I thought the defd.promise() returned an actual promise or at least something that could be cast to one... Is that not the case for this?

@Euregan
Copy link

Euregan commented Mar 6, 2018

Just wanted to point out that Chrome seems to wrap the return in a Promise by default, while Firefox simply resolves to undefined. Works like a charm when wrapping the return in a Promise though.

@Rob--W
Copy link
Member

Rob--W commented Mar 6, 2018

In Firefox, the polyfill doesn't do anything since the browser object already exists. In Chrome, the polyfill aims to mimic the promise-based browser API that Firefox supports.

defd.promise from jQuery is not a real promise. It is a thenable.

@Rob--W
Copy link
Member

Rob--W commented Jul 5, 2018

We will document more clearly that the polyfill does not do anything in Firefox (#55).
To fix this specific bug, real promises should be used instead of thenables.

@Rob--W Rob--W closed this as completed Jul 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants