1

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?

  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – str May 03 '19 at 12:20
  • You could use `async` syntax instead which returns something. – Daniel W. May 03 '19 at 12:21

2 Answers2

1

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
0

then method doesn't return anything.

Try this:

var myvariable;
module.someMethod.then(value => {
    myvariable = value;
    makeSomethingWith();
});
FrV
  • 260
  • 2
  • 9