0

//Client.js

var CON,FUNC;
class FUNCS {
  constructor() {}
   async createAccount(coinId){
    await new Promise(function(resolve, reject) {
      CON.openAccount(coinId).then(response => {
        console.log(response); //returns undefined
        resolve(response);
      })
    });
  }
}
class CONNECTION {   
  constructor() {this.socket = io.connect()}
   async openAccount(id){
   await Promise.resolve(this.socket.emit('openAccount',{'id':id},function (rs) { 
      return rs;
    }));
  }
}
CON = new CONNECTION(),FUNC = new FUNCS();

//index.html

<span onClick="FUNC.createAccount()"></span>

app.js

socket.on('openAccount',function (id,fn) {
    setTimeout(function () {
        fn('seee');
    },3000)
});

how can i catch callback from app.js in FUNCS.createAccount's console Because its giving undefined

CON.openAccount(coinId).then(response => {   
  **console.log(response);//returns undefined;**
  resolve(response);
})
2
  • Why are you using uppercase for class, variable and methods? Any particular reason? Commented May 9, 2020 at 21:28
  • it's a kind of habit Commented May 9, 2020 at 21:59

2 Answers 2

1

Let's keep the code style out of the discussion and have a look at the openAccount method:

  • If you are willing to grab something out of your method you need to return something. As it is defined, openAccount returns undefined (It's just waiting for the anonymous Promise and then returns nothing)

  • Even returning the promise (as expected by the await construct) would not work here, because it actually resolves the result of the socketIo emit method which returns undefined as well (because it is built to work with a callback)

Here is a simplified working exemple where socketIo is put appart and replaced by setTimeout:


async function resolveIfNumber(x) {
  return new Promise((resolve, reject) => {
    setTimeout(() => Number(x) === x ? resolve(x) : reject("not a number"))
  })
}

resolveIfNumber("1").then(console.log, console.error)
// console.error("not a number")
resolveIfNumber(1).then(console.log, console.error)
// console.log(1)

You might want to read this quick introduction to the subject for further explanation

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer; i saw many example about async/await but, i found its not working in socket.io emit await Promise.resolve(this.socket.emit('openAccount',{'id':id},function (rs) { return rs; })); how can i solve this with async system
As stated in the answer your openAccount method should look more like this: return new Promise((resolve) => this.socket.emit('openAccount', { id }, rs => resolve(rs))
0

i solved

//FUNC's Class

async createAccount(id){
return new Promise(function(resolve, reject) {
    CON.openAccount(id).then(response => {
      console.log(response); //returns response
      resolve(response);
    })
  });
}

//CONNECTION's Class

async openAccount(id){
  return new Promise((resolve, reject) => {
    this.socket.emit('openAccount',{'id':id},function (rs) {
        resolve(rs);
    })
  })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.