How to return value from an async function in node js and save to a variable inside jenkins pipeline script?

await request node js
promisify async/await

Calling Node js script in Jenkins pipeline script as follows: def result = bat node abc/xyz.js result.id to be used later in the pipeline script

Inside xyz.js, I defined a function and returning value as follows:

function sampleFunc(){
    // func2 is an async function
    func2()
    .then(results) => {
       // below console stmt is able to print results
       console.log(results)
       return results
    })
}

console.log(sampleFunc())

console log prints undefined.

Like n9iels said, you forgot to return a value. Another (slightly shorter) example below:

function func2() {
  // Mimic async function
  return new Promise(resolve => {
    setTimeout(() => {
      resolve("Hello World!");
    }, 1000);
  });
}

func2().then(results => {
  console.log(results);
});

Save Async/Await response on a variable, According to the document of JS, an async function will only return a Promise object instead of value. you have access and change the variable you want to assign within the async function instead of return from it. //Save response on a variable var globalData; const asyncExample = async () =>{ try { const� Pipeline supports two syntaxes, Declarative (introduced in Pipeline 2.5) and Scripted Pipeline.Both of which support building continuous delivery pipelines. Both may be used to define a Pipeline in either the web UI or with a Jenkinsfile, though it’s generally considered a best practice to create a Jenkinsfile and check the file into the source control repository.

The reason why sampleFunc() is returning undefined is that it is actually returning nothing. You are returning a value inside the then part of the async func2(). Since you are not returning func2 itself, sampleFunc has no return value.

A correct implementation would be:

function sampleFunc(){
    // func2 is an async function
    return func2().then(results) => {
        // below console stmt is able to print results
        console.log(results)
        return results
    })
}

// func2 and thus sampleFunc are async. So the return value will be a promise and not the actual return value
sampleFunc().then(function(result) { console.log(result); })

Promises can be really confusing when you use them the first time. You can read more about promises here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises

Edit: The example above will work if func2() has a correct Promise implementation and called its resolve function.

How to put wait in mocha nodejs - node.js - html, How to return value from an async function in node js and save to a variable inside jenkins pipeline script? - node.js. When it comes to Node.js there are a fair amount of solutions to this problem both built into the language and by the community. Let’s take a look at some of the most popular ones. We’ll be using NASA’s Astronomy Picture of the Day API as the JSON API that we are interacting with in all of these examples because space is the coolest thing

If you're using Node.js 8 or newer, you can use async/await:

function sampleFunc(){
    // func2 is an async function
    func2()
    .then(results) => {
       // below console stmt is able to print results
       console.log(results)
       return results
    })
}

async function main() {
    console.log(await sampleFunc())
}

main();

How to return the result of an asynchronous function in JavaScript, How to return value from an async function in node js and save to a variable inside jenkins pipeline script? Calling Node js script in Jenkins pipeline script as � To set the value of this to a particular value when calling a function, use call(), or apply() as in the following examples.. Example 1 // An object can be passed as the first argument to call or apply and this will be bound to it. var obj = {a: 'Custom'}; // We declare a variable and the variable is assigned to the global window as its property. var a = 'Global'; function whatsThis() { return

Groovy Language Documentation, How to return the result of an asynchronous function in JavaScript in it (for example a fetch() call), and can't directly return the result value. How to write Angular JS function to return value Posted on January 30, 2018 by Badshah Tracker Problem Statement: I have an angular js function that fetches data from a service, I am concatenating some columns and then return the concatenated data.

javascript return value from async function Code Example, Return type inference; Parameter type inference User Profile Scripts and State JmxBuilder.bean() Syntax; Bean() Node - Specifying MBean ObjectName Multiline comments can thus be put at the end of a statement, or even inside a can be used to store a reference to a method in a variable, in order to call it later:. The Node.js process will exit on it's own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully. Let’s cover possible reasons why you might be willing to exit node.js process and why you should avoid process.exit():

Changelog Archive, //you can only return a value from an async function by passing in a callback function like so: 2 Programa��o web com Node e Express Beneficiando-se da stack 'react-scripts' n'est pas reconnu en tant que commande interne ou It looks like you are passing several store enhancers to createStore(). page seems to work fine when running it on browser but when I am calling the same page in nodejs by requestjs and http.get, a function that supposes to run on that page is not executing. I want to call that page from the backend

Comments
  • you already concluded that the sampleFunc() returns undefined, so the problem occurs inside that function. Without knowing the implementation helping will be quite difficult for us.... So please share it :-)
  • @n9iels Added more details now. No issue with func2 as results are retrieved properly. Looks like the issue is with how I return it
  • sampleFunc has no defined return value. If func2 is async then sampleFunc will immediately return.
  • @jeff Got it. How do I return the value of func2 ?
  • I am able to print the result inside sampleFunc() now. I added stmt return results inside sampleFunc(). However def result = bat node abc/xyz.js still returns null object