Basically, your *getValue* method is returning two types of values; either
your desired value or a Promise. Since you're using the asyncronous GM API,
you should discard the old API because there's no way to make the two work
the same without dealing with Promises.
The *async* keyword "promisifies" functions, meaning they'll return
Promises, so the following two functions are the same:
async function myFunc() { ... }
function myFunc() { return new Promise(...) }
Promises either resolve or reject with a value. To get the value just call
the Promise's *then *method*.*
var promise = myFunc()
promise.then(value => ...)
If I were to rewrite your code, it would be as follows:
rt = await GM.getValue(name, defval);
On Sunday, October 8, 2017 at 7:12:17 AM UTC-7, Stig Nygaard wrote:
>
> I am trying to wrap GM4's asynchronous methods into synchronous methods
> for a fast and easy way to make my current userscripts "cross-API
> compatible".
>
> I'm not familiar with asynchronous Javascript, but by reading som posts on
> subject I thought I had found out how to achieve what I wanted. But it
> doesn't work :-/ ...
>
> When running below code in GM3 it alerts "value=42" (as expected). But
> when running on GM4 it alerts "value=[object Promise]".
> What am I doing wrong? How do I get rid of that Promise?...
>
> [ I have trouble posting in this forum. Maybe it doesn't like the code I'm
> posting or my posts get to big? So now posting a link to code instead: ]
>
> https://gist.github.com/StigNygaard/ea03b7ad2faeabe732c308786e3046c9
>
> I know it would be better to try to take advantage of the possibilities of
> asynchronous code, but that must be a project for a later time. Right now
> I'm just looking for the quick and easy fix.
>
> /Stig.
>
On Sunday, October 8, 2017 at 7:12:17 AM UTC-7, Stig Nygaard wrote:
>
> I am trying to wrap GM4's asynchronous methods into synchronous methods
> for a fast and easy way to make my current userscripts "cross-API
> compatible".
>
> I'm not familiar with asynchronous Javascript, but by reading som posts on
> subject I thought I had found out how to achieve what I wanted. But it
> doesn't work :-/ ...
>
> When running below code in GM3 it alerts "value=42" (as expected). But
> when running on GM4 it alerts "value=[object Promise]".
> What am I doing wrong? How do I get rid of that Promise?...
>
> [ I have trouble posting in this forum. Maybe it doesn't like the code I'm
> posting or my posts get to big? So now posting a link to code instead: ]
>
> https://gist.github.com/StigNygaard/ea03b7ad2faeabe732c308786e3046c9
>
> I know it would be better to try to take advantage of the possibilities of
> asynchronous code, but that must be a project for a later time. Right now
> I'm just looking for the quick and easy fix.
>
> /Stig.
>
>
--
You received this message because you are subscribed to the Google Groups
"greasemonkey-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/greasemonkey-users.
For more options, visit https://groups.google.com/d/optout.