Javascript 承诺中的Twilio方法不起作用
阅读 我尝试在我的Laravel 8/jQuery 3.5.1/vue 2.6应用程序中实现聊天 本文件定义了:Javascript 承诺中的Twilio方法不起作用,javascript,laravel,vue.js,twilio,Javascript,Laravel,Vue.js,Twilio,阅读 我尝试在我的Laravel 8/jQuery 3.5.1/vue 2.6应用程序中实现聊天 本文件定义了: setupChannel(channel){ let vm = this; return this.leaveCurrentChannel() .then(function() { return vm.initChannel(channel); }) .then(function(_channel) {
setupChannel(channel){
let vm = this;
return this.leaveCurrentChannel()
.then(function() {
return vm.initChannel(channel);
})
.then(function(_channel) {
return vm.joinChannel(_channel);
})
.then(this.initChannelEvents);
},
我想扩展joinChannel方法,因为我想检查当前登录的用户(laravel auth)
已加入。我试着用承诺和失败来实现它,就像我内心的代码一样
vm.tc.messagingClient.getSubscribedUsers() is not run. I do
setupChannel(channel){
let vm = this;
return this.leaveCurrentChannel()
.then(function() {
return vm.initChannel(channel);
})
.then(function(_channel) {
let promise = new Promise((resolve, reject) => {
// debugger
vm.tc.messagingClient.getSubscribedUsers().then(function(users) {
// THESE CODE IS NOT RUN. If to uncomment console and debugging it is not triggered
// console.log('++ users::')
// console.log(users)
// debugger
for (let i = 0; i < users.length; i++) {
const user = users[i];
console.log('user.identity: ' + JSON.stringify(user.identity) );
// console.log('user: ' + JSON.stringify(user, null, 2) );
if( user.identity === vm.loggedUser.name ) {
resolve("result")
}
}
debugger // THESE CODE IS NOT RUN
resolve("error")
})
})
console.log('++ promise::')
console.log(promise) // I SEE this promise in pending state
promise
.then(
result => {
alert("result: " + result);
return _channel;
},
error => {
alert("error: " + error);
return vm.joinChannel(_channel);
}
)
// return vm.joinChannel(_channel);
})
.then(this.initChannelEvents);
在promise内部,它工作正常,我得到了有效的结果
我的承诺结构有什么问题,我如何修复
修改的块:
我试着按照你的方式:
joinGeneralChannel() {
console.log('Attempting to join "general" chat channel...');
let vm = this;
if (this.tc.generalChannel == null) {
console.log('SETTING this.tc.messagingClient.createChannel')
vm.loadChannelList(vm.joinGeneralChannel);
}else {
// console.log('Found general channel:');
this.setupChannel(this.tc.generalChannel);
}
},
async setupChannel(channel) {
let vm = this
await this.leaveCurrentChannel()
const newChannel = await vm.initChannel(channel)
const subscribedUsers = vm.tc.messagingClient.getSubscribedUsers()
console.log('subscribedUsers::')
console.log(subscribedUsers)
let isUserJoined = false
for (let i = 0; i < subscribedUsers.length; i++) {
console.log('subscribedUsers[i] ' + JSON.stringify(subscribedUsers[i]) );
if( subscribedUsers[i].name === vm.loggedUser.name ) {
isUserJoined = true``
break
}
}
debugger
console.log('isUserJoined::')
console.log(isUserJoined)
joinGeneralChannel(){
log('试图加入“常规”聊天频道…');
让vm=这个;
if(this.tc.generalChannel==null){
console.log('设置此.tc.messagingClient.createChannel')
loadChannelList(vm.joinGeneralChannel);
}否则{
//log('Found-general channel:');
this.setupChannel(this.tc.generalChannel);
}
},
异步设置通道(通道){
让vm=这个
等待此消息。leaveCurrentChannel()
const newChannel=等待vm.initChannel(通道)
const subscribedUsers=vm.tc.messagingClient.getSubscribedUsers()
console.log('subscribedUsers::')
console.log(订阅用户)
设isUserJoined=false
for(设i=0;i
但在我的浏览器的cosole中,我看到:
Initialized channel General Channel
TeamChat.vue?e1c8:561 subscribedUsers::
TeamChat.vue?e1c8:562 Promise {<pending>}__proto__: Promise[[PromiseState]]: "pending"[[PromiseResult]]: undefined
TeamChat.vue?e1c8:573 isUserJoined::
初始化通道常规通道
TeamChat.vue?e1c8:561订阅用户::
TeamChat.vue?e1c8:562承诺{}\uuuuu协议:承诺[[PromiseState]]:“待定”[[promisersult]]:未定义
TeamChat.vue?e1c8:573 IsUserJourned::
看起来getSubscribedUsers方法是异步的
谢谢!可能您的承诺失败了,这就是为什么
then()
永远不会执行的原因。要扩展joinChannel
方法,您可以使用async/Wait和ES6语法执行类似操作:
async setupChannel(channel) {
let vm = this;
try {
await this.leaveCurrentChannel();
const newChannel = await vm.initChannel(channel);
const users = await vm.tc.messagingClient.getSubscribedUsers();
const isUserJoined = users.some(({ name }) => name === vm.loggedUser.name);
const joinedChannel = isUserJoined ? newChannel : vm.joinChannel(_channel);
return this.initChannelEvents(joinedChannel);
} catch(err) {
// if some of promises below will fail, here you'll see details
console.log('Issue details here:', err);
}
}
可能您的承诺失败了,这就是为什么
then()
永远不会执行的原因。要扩展joinChannel
方法,您可以使用async/await和ES6语法执行以下操作:
async setupChannel(channel) {
let vm = this;
try {
await this.leaveCurrentChannel();
const newChannel = await vm.initChannel(channel);
const users = await vm.tc.messagingClient.getSubscribedUsers();
const isUserJoined = users.some(({ name }) => name === vm.loggedUser.name);
const joinedChannel = isUserJoined ? newChannel : vm.joinChannel(_channel);
return this.initChannelEvents(joinedChannel);
} catch(err) {
// if some of promises below will fail, here you'll see details
console.log('Issue details here:', err);
}
}
我做了更多的检查和调试,我看到所有参数都是有效的:我的意思是无效变量或无效作用域不是问题。但我不明白为什么vm.tc.messagingClient.getSubscribedUsers()中的代码。然后(函数(用户)没有运行。这个循环在一个承诺内正常工作…我的承诺无效吗?我做了更多的检查和调试,我看到所有参数都是有效的:我的意思是无效变量或无效范围不是问题。但我不明白为什么vm.tc.messagingClient.getSubscribedUsers()内的代码。然后(函数(用户)未运行。此循环在承诺内正常工作…我的承诺无效吗?请提供一个关于getSubscribedUsers的引用链接?我在这里找到了描述:它说:函数getSubscribedUsers()返回所有当前订阅用户的数组,我在其中未同步找到任何用户…抱歉,我错了,因为我在中找到了此便笺(在“getSubscribedUsers”页上查找),谢谢!看起来它可以工作!我现在测试它。您能解释一下:1)Twilio聊天API(at)中的所有方法是否都是异步的吗?2)我知道什么是异步请求,比如axios,ajax。然后是成功。你能解释一下什么时候必须使用承诺或等待调用吗?或者在这个问题上的好文章吗?这相当令人困惑。1.Twilio文档看起来不太友好,所以我不能确定,但你可以检查并看看每个meth返回了什么例如,您可以看到它返回的是
Promise.
2.基本上是Promise和async/await。此操作“可以通过Ajax或WebSocket连接在internet上获取数据,从数据库(如MongoDB)查询数据,或使用NodeJs“fs”模块访问文件系统”,你能给我一个关于getSubscribedUsers的链接吗?我在这里找到了描述:它说:函数getSubscribedUsers()返回所有当前订阅用户的数组,我在其中没有同步找到任何用户…抱歉,我错了,因为我在(在“getSubscribedUsers”页上查找)中找到了此注释谢谢!看起来很有效!我现在正在测试它。你能解释一下吗:1)如果Twilio聊天API(at)中的所有方法是异步的吗?2)我知道什么是异步请求,比如axios,ajax。那么在成功的时候。你能解释一下什么时候必须使用承诺或等待调用吗?或者关于这个问题的好文章吗?这相当令人困惑……1.Twilio文档看起来不太友好,所以我不能肯定,但你可以检查一下,看看有什么内容URN每个方法。例如,您可以看到它返回Promise.
2。基本上是Promise和async/wait。此操作“可以通过Ajax或WebSocket连接在internet上获取数据,从数据库(如MongoDB)查询数据,或使用NodeJs“fs”模块访问文件系统”,