Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

javascript - 异步/等待/返回不等待响应(Async / Await / Return NOT Waiting on Response)

I have a function that loops through a list of phone numbers.(我有一个遍历电话号码列表的功能。)

if one of them match the criteria, it records the information on a table and then sends an SMS and search if another number meets the criteria too.(如果其中一个符合条件,则它将信息记录在表上,然后发送SMS并搜索另一个数字是否也符合条件。) To achieve this, I'm using Async/Await, but my problem is that the last function, which sends the SMS, I have a console.log and then it has a return;(为此,我使用了Async / Await,但是我的问题是发送短信的最后一个函数是console.log ,然后返回了它。) I'm getting the log in the right order, but it's not waiting for a response of the return.(我以正确的顺序获取日志,但是它没有等待返回的响应。) Here's my code.(这是我的代码。) return t.any(Query).then(function(Response){ const getQualifiedUsers = async() => { try{ if("meets criteria"){ try{ await insertInTable() }catch(error){ console.log(error); } } }catch(error){ console.log(error); } } getQualifiedUsers(); } async function insertInTable(){ try{ await db.any(secondQuery).then(function(numberSaved){ const sendAsyncSMS = async () => { try{ await sendSMS(); }catch(error){ console.log(error); } } sendAsyncSMS(); } }catch(error){ console.log(error); } } async function sendSMS() { var date = moment().utc().format('MM-DD-YYYY HH:mm:ss'); console.log('date', date); return client.messages.create(params, function(err, data, message) { if(err){ return 'done' }else{ return 'done' } }) .then(message => console.log(message.sid) ); } With this, I'm getting as a response search-->insert-->log date and then goes back to search instead of sending the message.(有了这个,我得到一个响应search-->insert-->log date ,然后返回搜索而不是发送消息。) After it finishes, it tries to send all SMS.(完成后,它将尝试发送所有SMS。) So it's not waiting for a response on the return .(因此,它不是在等待return的响应。) I've changed this a lot of times but I can't seem to figure out what am I missing and why am I not being able to wait for the response.(我已经对此进行了很多次更改,但是我似乎无法弄清我想念的是什么以及为什么我不能等待响应。)   ask by MCM13 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason it doesn't wait for the await is because your asynchronous function is called synchronously (without await) getQualifiedUsers() , so it runs it, but doesn't wait for it to finish.(它不等待等待的原因是因为您的异步函数被同步调用(没有等待) getQualifiedUsers() ,因此它运行它,但不等待它完成。)

Since it seems like you are working with a promise you can change this:(由于似乎您正在兑现承诺,因此可以更改此设置:) return t.any(Query).then(function(Response){ const getQualifiedUsers = async() => { try{ if("meets criteria"){ try{ await insertInTable() }catch(error){ console.log(error); } } }catch(error){ console.log(error); } } getQualifiedUsers(); } into this:(到这个:) return t.any(Query).then(async function(Response) { try { if ("meets criteria") { try { await insertInTable() } catch(error) { console.log(error); } } } catch(error) { console.log(error); } } and it should work correctly.(它应该可以正常工作。) EDIT: Oops I missed something in your insertInTable() , basically the same thing so change that to the following:(编辑:糟糕,我错过了您的insertInTable()中的insertInTable() ,基本上是同一件事,因此将其更改为以下内容:) async function insertInTable(){ try { await db.any(secondQuery).then(async function(numberSaved) { try { await sendSMS(); } catch(error){ console.log(error); } } } catch(error) { console.log(error); } }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...