How to return value from a promise

const getVideoCount = () => {
      VideoStore.setProvider(web3.currentProvider);
      const instance = VideoStore.deployed().then(instanceRes => {
        const accounts = web3.eth.getAccounts().then(instanceAcc => {
          const count = instanceRes.getVideoListCount
            .call({
              from: instanceAcc[0]
            })
            .then(res => {
              console.log(res["words"][0]);
            });
        });
      });
    };

I want the value of console.log(res["words"][0]);
How do I do that?

Edit: Code formatting

console.log doesn’t return a value at all. If you’re referring to getting the value that you’re logging, you have to use it in the .then callback, e.g.

            .then(res => {
              console.log(res["words"][0]);
              do_something_with(res["words"][0];
              ...

There’s no way to otherwise make a Promise “return” something back to synchronous code – once you start using them, you’re stuck with them.

That’s true, but you can keep chaining as many promises as you like using then.

new Promise(resolve => setTimeout(() => resolve(6), 1000))
  .then(res => res * 7)
  .then(newRes => console.log(newRes)) // logs 42 after 1 second
1 Like

@lionel-rowe @chuckadams I solved it by passing the res["words"][0] value to a function and returning that value. It is sort of a hack/workaround, and thought you guys should know I figured it out.
Cheers