Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stuck within await()... #64

Closed
MatthieuHPP opened this issue Oct 20, 2017 · 2 comments
Closed

Stuck within await()... #64

MatthieuHPP opened this issue Oct 20, 2017 · 2 comments

Comments

@MatthieuHPP
Copy link

Hello,

I am trying to use asyncawait and do not manage to make it works. I apologize for my question which answer should be obvious...
The part of the program after await is not executed at all. What should I do to leave the function concerned by await once it has been executed ?

If required (node v6.11 & asyncawait@lastest)

Many thanks

 joinArrayRooms(roomNames)

const joinArrayRooms = async (function(roomNames){
          let newRoomNames = []
            console.log('roomNames : ',roomNames)
        await(function(){
          for(let i_roomNames=0;i_roomNames<roomNames.length;i_roomNames++){

          socket.join(roomNames[i_roomNames],function(){
            newRoomNames.push(roomNames[i_roomNames])
              // send to peers my new info
              console.log('roomName : ', roomNames[i_roomNames])
            socket.broadcast.to(roomNames[i_roomNames]).emit('sendRoomData','test_data')
          })
         }
        })
        // THIS PART IS NOT EXECUTED
        // update array of rooms on client side
        console.log('joinRoom_emit : ',newRoomNames)
        return socket.emit('joinRoom',newRoomNames)
        // TODO store the data in the dB

      })

 
@yortus
Copy link
Owner

yortus commented Oct 25, 2017

HI @MatthieuHPP,

The problem is with the await(...) call. The await function is expecting a promise as an argument, but you are passing it a function. This invokes some little-used behaviour in this library (not the behaviour you were intending) and in short joinArrayRooms never resumes.

The proper way to do this it to take out the await call around the for loop, and put await calls around the things that are async, like socket.join(...). But for that to work, you need socket.join to return a promise (you can use promisify for that).

@MatthieuHPP
Copy link
Author

Many thanks for your help and accurate explanations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants