Javascript await promise question

I’m trying to implement push notifications using OneSignal.

I have the main part of the setup working I’m now just trying to write and read external user id.

I’m stuck with trying to get a return into Anvil from the following :

OneSignal.push(function() {
  OneSignal.getExternalUserId().then(function(externalUserId){
    console.log("externalUserId: ", externalUserId);
  });
});

I believe I would need to use anvil.js.await_promise(js_promise)

I just don’t know how to format it with a nested javascript function.

Just a heads up, my python knowledge is rudimentary, and my javascript is worse so please bear that in mind when responding.

OneSignal.push(function() {
  return OneSignal.getExternalUserId().then(function(externalUserId){
    console.log("externalUserId: ", externalUserId);
    return someOperationOnexternalUserId;
  });
});

I believe you need to return the value from the inner function to the outer function and then return that value.

Thanks for the reply. Still a little unsure how to use this. Could you explain the meaning of someOperationOnexternalUserId.

I have tried the following:

In Native Libraries:

  function OS_get_ext_uid() {
  	return OneSignal.push(function() {
  		return OneSignal.getExternalUserId().then(function(externalUserId){
  			return externalUserId;
  		});
  	});
  }

Called from Anvil with:

OS_ext_ID = str(js.call_js('OS_get_ext_uid'))
alert_text = "OS User ID: " + OS_ext_ID
alert(alert_text)

But it’s giving me a ‘None’ value.

As you can probably see I’m not familiar with nested functions and promises vs values. But I know more today than I did at the start of yesterday so that’s always good.

I have made a little progress but still would like to know how to get the nested returns above working.

So the progress made and additional questions are as follows:

  1. adding an alert in the JS function shows me the correct External User ID so the function itself is working it’s just the nested function return back to Anvil that appears to be failing:
function OS_get_ext_uid() {
  	return OneSignal.push(function() {
  		return OneSignal.getExternalUserId().then(function(externalUserId){
            const alert_text = `Ext UID - ${externalUserId}`;
            alert(alert_text);
  			return externalUserId;
  		});
  	});
  }
  1. Calling OneSignal.getExternalUserId() directly gives me the correct external user Id and returns correctly to Anvil:
  function OS_get_ext_uid2() {
  	return OneSignal.getExternalUserId();
  1. I’m assuming from the following excerpt from the OneSignal docs that the direct call is actually a synchronous call not asynchronous. Should that concern me?:

Our API functions can be called asynchronously using either:

  1. OneSignal.push(["functionName", param1, param2]);
  2. OneSignal.push(function() { OneSignal.functionName(param1, param2); });

Option 2 must be used for functions that return a value like isPushNotificationsSupported. Option 2 also lets you call as many OneSignal functions as you need inside the passed-in function block.

I’ll keep plugging away but any guidance would be great.