Waiting for JS action return value

0
Hi all, I have a nanoflow which calls a javascript action – this action returns a string. My nanoflow uses this return value to update an attribute of the input entity. But since, js actions are async, the attribute value is always ending up with a null. How do I wait for the js action to complete and use the return value to update my attribute? Please help! Thank you! - Kamala  
asked
2 answers
1

So your Javascript action should contain something like:

function getFromExternalSource() {
    // BEGIN USER CODE

    // We're returning a Promise, which will resolve with a value
    return new Promise(function (resolve, reject) {
        // We call the external library with method getValue. getValue expects a callback
        somexternalThingy.getValue(function (returnValue) {
            // When the value comes from this async function callback, we resolve the value
            resolve(returnValue);
        })
    })
    // END USER CODE
}

 

answered
0

Hi Kamala,

You need to make use of promises for this. Instead of returning a string directly, you resolve a promise when the result is available. Please see the following pages for more information:

See the 8th bullet in the last link for an example of how to use a promise to return an asynchronous result.

answered