danielwerg / r6api.js

🍫 Node.js wrapper around Rainbow Six Siege APIs
https://npm.im/r6api.js
MIT License
111 stars 19 forks source link

[Not a problem of a package] #45

Closed ghost closed 3 years ago

ghost commented 3 years ago
const R6API = require('r6api.js');
const r6api = new R6API('mail@mail.com', 'password');

const username = 'wikipedia.org', platform = 'uplay';

var r6temp = (async function(platform, username){
     const id = await r6api.getId(platform, username).then(el => el[0].userId);
     return id; 
})(platform, username);
var id = r6temp;

console.log('User Id of ${username} is ${id}');

this code outputs

[object Promise]

but by

const id = await r6api.getId(platform, username).then(el => el[0].userId);

this code will show me my userid (of course only in runkit, i get "await is only valid in async function" on my console)

Can someone help me how to make my code show my userid?

EndBug commented 3 years ago

You need to use the Promise correctly. One way to do it is:

r6api
  .getId(platform, username)
  .then(el => el[0].userId)
  .then(console.log)

If you've never worked with asynchronous code yet, it's better if you watch/read a couple of guides, since it's a broad subject and it would be difficult to teach you everything in this issue.

ghost commented 3 years ago

I've done that too and it gave me the same result (of course the code was ran on runkit, so it's simplified)

ok, i am using this package with discord.js. the id shows properly only in console in asynchronous function. But it does not show properly on discord message. Maybe i'll review my full code again until i find why.

Thank you for your help anyway.