user11447349
user11447349

Reputation:

Node / JS Promise then pass to variable not passing

I have a promise in a class that looks like this:

someMethod() {
    return new Promise(function(resolve) {
         resolve(10);
    }
}

Below I know that value WILL return 10 but I want to pass it to myvariable so I've done this:

var myvariable = module.someMethod.then(value => {
    return value;
});

But it's not passing the value.

How can I do this?

Upvotes: 1

Views: 30

Answers (2)

Arvind Rajput
Arvind Rajput

Reputation: 114

you can do this like

    function someMethod() {
        return new Promise(function (resolve) {
            resolve(10);
        })
    }

    async function test() {
        var myVar = await someMethod();
        console.log(myVar)
    }
if you call the test function in myVar you will get 10

Upvotes: 1

FrV
FrV

Reputation: 258

then method doesn't return anything.

Try this:

var myvariable;
module.someMethod.then(value => {
    myvariable = value;
    makeSomethingWith();
});

Upvotes: 0

Related Questions