admin管理员组文章数量:1023057
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
1 Answer
Reset to default 8I 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);
}
});
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
1 Answer
Reset to default 8I 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);
}
});
本文标签: javascriptTypescript AsyncAwait with SignalR FunctionStack Overflow
版权声明:本文标题:javascript - Typescript AsyncAwait with SignalR Function - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745544677a2155341.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论