javascript - Typescript AsyncAwait with SignalR Function - Stack Overflow

I have encountered a problem where a sequence of SignalR hub function fails to be executed asynchronous

I have encountered a problem where a sequence of SignalR hub function fails to be executed asynchronously.

I have:

//Hub functions
//Initializing hub server and clients.
function HubStart() {
    $.connection.hub.start().then(function () {
        console.log(1);
        return new Promise(resolve => resolve);
    });
}
//Hub server-side function that add user's name to chat board.
function HubUserOnline(user: any) {
    $.connection.boardHub.server.userOnline(user).then(resolve => { return new Promise(resolve => resolve); });
}

//Main
var viewModel = ko.mapping.fromJS(model, mappingOption);
main();

//Definition of the main function
async function main() {
    console.log(0);
    await HubStart();
    console.log(2);
    await HubUserOnline(ko.mapping.toJS(viewModel.CurrentUser))
    console.log(3);
    }
});

However, the console says:

> 0
> 2
> Uncaught (in promise) Error: SignalR: Connection must be started before data can be sent. Call .start() before .send()
>   at hubConnection.fn.init.send (jquery.signalR-2.2.1.js:780)
>   at init.invoke (jquery.signalR-2.2.1.js:2734)
>   at Object.userOnline (hubs:120)
>   at HubUserOnline (WaitingRoom.ts:190)
>   at WaitingRoom.ts:203
>   at Generator.next (<anonymous>)
>   at fulfilled (WaitingRoom.ts:1)
> 1

Thus it indicates the second hub function was going to be executed before the hub instance had been created and it got an error.

The hub functions return JQueryPromise so I tried to let a function return a promise when the hub functions are pleted. Can anybody indicate a fault in my code and trial?

I have encountered a problem where a sequence of SignalR hub function fails to be executed asynchronously.

I have:

//Hub functions
//Initializing hub server and clients.
function HubStart() {
    $.connection.hub.start().then(function () {
        console.log(1);
        return new Promise(resolve => resolve);
    });
}
//Hub server-side function that add user's name to chat board.
function HubUserOnline(user: any) {
    $.connection.boardHub.server.userOnline(user).then(resolve => { return new Promise(resolve => resolve); });
}

//Main
var viewModel = ko.mapping.fromJS(model, mappingOption);
main();

//Definition of the main function
async function main() {
    console.log(0);
    await HubStart();
    console.log(2);
    await HubUserOnline(ko.mapping.toJS(viewModel.CurrentUser))
    console.log(3);
    }
});

However, the console says:

> 0
> 2
> Uncaught (in promise) Error: SignalR: Connection must be started before data can be sent. Call .start() before .send()
>   at hubConnection.fn.init.send (jquery.signalR-2.2.1.js:780)
>   at init.invoke (jquery.signalR-2.2.1.js:2734)
>   at Object.userOnline (hubs:120)
>   at HubUserOnline (WaitingRoom.ts:190)
>   at WaitingRoom.ts:203
>   at Generator.next (<anonymous>)
>   at fulfilled (WaitingRoom.ts:1)
> 1

Thus it indicates the second hub function was going to be executed before the hub instance had been created and it got an error.

The hub functions return JQueryPromise so I tried to let a function return a promise when the hub functions are pleted. Can anybody indicate a fault in my code and trial?

Share Improve this question asked Apr 16, 2017 at 21:08 kemakinokemakino 1,1221 gold badge14 silver badges38 bronze badges 1
  • Promises are regular objects. You need to return them from the function if you want to use them, otherwise, your functions just return undefined. – Madara's Ghost Commented Apr 16, 2017 at 22:14
Add a ment  | 

1 Answer 1

Reset to default 8

I believe the problem is that HubStart and HubUserOnline are not returning a promise and instead you fell in a promise antipattern.

Try the following:

//Hub functions
//Initializing hub server and clients.
function HubStart() {
  return new Promise((resolve, reject) => {
    $.connection.hub.start().then(() => {
      console.log(1);
      resolve();
    });
  });
}
//Hub server-side function that add user's name to chat board.
function HubUserOnline(user: any) {
  return new Promise((resolve, reject) => {
    $.connection.boardHub.server.userOnline(user).then(() => {
      resolve();
    });
  });
}

//Main
var viewModel = ko.mapping.fromJS(model, mappingOption);
main();

//Definition of the main function
async function main() {
  console.log(0);
  await HubStart();
  console.log(2);
  await HubUserOnline(ko.mapping.toJS(viewModel.CurrentUser))
  console.log(3);
}
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745544679a4632286.html

相关推荐

  • javascript - Typescript AsyncAwait with SignalR Function - Stack Overflow

    I have encountered a problem where a sequence of SignalR hub function fails to be executed asynchronous

    13小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信