Hi userscript coders

I'm not familiar with javascripts new asynchronous features, so I'm trying 
to wrap GM4's new asynchronous API into some simple to use synchronous 
methods to quickly update my userscripts for cross-API compatibility. I 
studied some posts on subject, and thought I understood what to do. But it 
doesn't work...

In the following I simply write a value with setValue and reads it again 
with getValue. With GM3 it will alert *"value=42"*, as expected. But with 
GM4 it alerts *"value=[object Promise]"*.

What am I doing wrong? How do I get rid of that Promise?

var *gm4test *= { 
 INFO: true, 
 DEBUG: false, 
 *log*: function(s, info) { 
 if ((info && window.console) || (gm4test.DEBUG && window.console)) { 
 window.console.log('*GM4test* '+s); 
 } 
 }, 
 *getValue*: function(name, defval) { 
 var rt; 
 if (typeof GM_getValue === 'function') { // GM3 etc 
 rt = GM_getValue(name, defval); 
 } else if (typeof GM === 'object' && typeof GM.setValue === 'function') { // 
GM4 
 rt = (*async *function(){return *await *GM.getValue(name, defval);})(); 
 } else { 
 alert('Sorry, no support for GM getValue API method'); 
 gm4test.log('Sorry, no support for GM getValue API method', gm4test.INFO); 
 } 
 return rt; 
 }, 
 *setValue*: function(name, value) { 
 if (typeof GM_setValue === 'function') { // GM3 etc 
 GM_setValue(name, value); 
 } else if (typeof GM === 'object' && typeof GM.setValue === 'function') { // 
GM4 
 (*async *function(){*await *GM.setValue(name, value);})(); 
 } else { 
 alert('Sorry, no support for GM setValue API method'); 
 gm4test.log('Sorry, no support for GM setValue API method', gm4test.INFO); 
 } 
 }, 
 *run*: function () { 
 gm4test.log('Running...'); 

 // Set... 
 gm4test.*setValue*('gm4val', 42); 
 // and get again... 
 var rv = gm4test.*getValue*('gm4val', ''); 

 *alert('value='+rv); // Expected "value=42", but with GM4 I get 
"value=[object Promise]" !* 
 } 
};

*gm4test.run();*


https://gist.github.com/StigNygaard/ea03b7ad2faeabe732c308786e3046c9

I know it would be more ambitious to try to take advantage of the new 
asynchronous possibilities, but that will have to be a project for later. 
Right now I'm just looking for a quick and easy fix for my scripts...

-- 
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.

Reply via email to