Home > database >  How to set .then to var? [duplicate]
How to set .then to var? [duplicate]

Time:09-28

So I am trying using a package called "app-store-scraper"

All of the examples look something like this:

var store = require('app-store-scraper');

store.app({id: 553834731}).then(console.log).catch(console.log);

but all it does is print it to the console. So I was wondering how to put it into a variable?

I have tried to do var test = store.app({id: 553834731}) but it returns nothing

Edit: I'm using this to return like res.end(); on a webserver. so I'm trying to return store.app()

CodePudding user response:

Your function - store.app() returns a promise.

you can do:

var test = store.app({id: 553834731})
test.then(console.log);

in a modern async/await syntax, which is a lot easier to understand your code can be rewritten:

try {
  const ret = await store.app({id: 553834731});
  console.log(ret);
}
catch (e) {
  console.log(e);
}

CodePudding user response:

let result;

store.app({ id: 553834731 }).then(res => result = res).catch(console.error);

or

const result = await store.app({ id: 553834731 });
  • Related