Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >for循环回调中的异步请求(facebook messenger bot)

for循环回调中的异步请求(facebook messenger bot)
EN

Stack Overflow用户
提问于 2018-01-11 14:41:00
回答 1查看 94关注 0票数 0

我正在尝试向我的订阅用户(+-8000)发送自动新闻消息,为此,我使用了一个for循环来传递每个用户。问题是我在for循环的异步函数的回调中使用了一个异步函数,但是我的bot发送了所有的文本,然后发送了所有的泛型,所以用户在收到文本和泛型之间可能需要8分钟的时间。

下面是我的代码:

代码语言:javascript
运行
AI代码解释
复制
for (user of userList) {
  sendText({
      id: user.id,
      text: Sometext
    },
    //////CALLBACK/////////

    (err, data) => {
      if (err) {
        console.err("text => " + err)
      }
      console.log(`Text send ${user}`)

      /////IT SHOULD SEND THE GENERIC EVERYTIME WHEN THE TEXT CALLBACK

      sendGeneric({
          id: user.id,
          elements: elements
        },
        //////CALLBACK/////////
        (err, data) => {
          if (err) {
            console.err("Generic => " + err)
          }
          console.log(`carousel  send ${user}`);
        })
    })
}

然后在我的日志里我得到了

代码语言:javascript
运行
AI代码解释
复制
Text send user1
Text send user2
Text send user4
Text send user5
Text send user3


carousel send user1
carousel send user4
carousel send user2
carousel send user5
carousel send user3
EN

回答 1

Stack Overflow用户

发布于 2018-01-11 23:17:59

那么,sendTextsendGeneric是异步函数吗?因为你从来不会等他们。

此外,如果您支持最新的ecmascript,您可以将for循环中的函数与异步封装在一起,然后等待每个sendText

代码语言:javascript
运行
AI代码解释
复制
var someFunc = async function() {
     //Some code ...
     for(user of userList){
         await sendText(/*your function*/);
     }
}

如果你不支持异步/等待的最新ecmascript,那么你可以为每次迭代链接队列(类似.then )

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48209650

复制
相关文章
servlet中的IllegalStateException
本文链接:https://blog.csdn.net/u014427391/article/details/97397116
SmileNicky
2019/08/29
5360
servlet系列之IllegalStateException
IllegalStateException在java web开发中比较常见,IllegalStateException的根本原因是java servlet在提交响应后,还尝试写内容。
SmileNicky
2022/05/07
1810
解决okhttp报java.lang.IllegalStateException: closed,java.lang.IllegalStateException: closed
在调用了response.body().string()方法之后,response中的流会被关闭,我们需要创建出一个新的response给应用层处理。不多说直接贴代码:
程思扬
2022/01/10
1K0
java.lang.IllegalStateException commit already called
#java.lang.IllegalStateException commit already called
程序员飞飞
2020/02/27
1.2K0
java.lang.IllegalStateException commit already called
解决:java.lang.IllegalStateException: ApplicationEventMulticaster not initialized
改了包名,以至于扫描包时找不到原路径。如红框中设置(原本叫 cn.xxx.mapper)。
微风-- 轻许--
2022/04/13
7790
解决:java.lang.IllegalStateException: ApplicationEventMulticaster not initialized
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration
版权声明:本文为博主原创文章,未经博主允许不得转载。 在SpringBoot项目中跑Junit单元测试发现此错 解决方案:把你的SpringBoot启动类放到项目包的根目录下  @Ru
DencyCheng
2018/11/05
3K0
DialogFragment IllegalStateException: Can not perform this action after onSaveIn
DialogFragment 偶现IllegalStateException: Can not perform this action after onSaveInstanceState。一般的解决方法,使用commitAllowingStateLoss 替代 commit,但DialogFragment的show方法默认使用的commit,无法修改
FangMessi
2021/09/28
9640
DialogFragment IllegalStateException: Can not perform this action after onSaveIn
java.lang.IllegalStateException: Duplicate key 20
这个我在公司遇到的一个问题。原因: 使用Map<String, String> RelationMap = relation.stream().collect(Collectors.toMap(s -> s[2], s -> s[1], (oldValue, newValue) -> newValue))) 转换过程中出现重复的Key。导致有多个value程序不知道应该取哪个的问题。
用户1518699
2019/08/08
1.6K0
RedisTemplate执行lua脚本抛出异常IllegalStateException
因为没有指定ReturnType,所以默认使用ReturnType.STATUS,返回值就是io.lettuce.core.output.StatusOutput,这个类并没有重写CommandOutput中的方法。所以抛出异常IllegalStateException
十毛
2019/10/24
6.4K0
nested exception is java.lang.IllegalStateException: refreshAfterWrite requires
已解决 nested exception is java.lang.IllegalStateException: refreshAfterWrite requires a LoadingCache异常解决
谙忆
2020/08/07
2K0
抛出IllegalStateException异常。Queue-PriorityQueue源码解析
add(E e) -> 往队列添加一个元素,如果队列已满抛出IllegalStateException异常。 offer(E e) -> 往队列添加一个元素,true成功,false失败,和add区别在与不会因为队列已满抛异常。 删除元素接口:
w4979的博客
2020/06/01
4460
抛出IllegalStateException异常。Queue-PriorityQueue源码解析
add(E e) -> 往队列添加一个元素,如果队列已满抛出IllegalStateException异常。
w4979的博客
2020/05/20
4860
非法的状态异常:java.lang.IllegalStateException commit already called
之所以报该错误,是因为 Fragment 事务是全局的变量,只能commit一次。
程序员飞飞
2022/05/09
4990
非法的状态异常:java.lang.IllegalStateException commit already called
ButterKnife Fragment java.lang.IllegalStateException: Bindings already cleared.
使用 ButterKnife 从 7.x.x 升级到 10.x.x 后,某个 viewPager 中的
Seachal
2021/01/28
5590
IDEA热布署报错java.lang.IllegalStateException
在IDEA导入devtools依赖实现热布署,启动后遇到如下问题 java.lang.IllegalStateException: Restarter has not been initialized at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.boot.devtools.restart.Rest
道可道非常道
2019/05/06
1.3K0
IDEA热布署报错java.lang.IllegalStateException
java.lang.IllegalStateException: Restarter has not been initialized
在IDEA导入devtools依赖实现热布署,启动后遇到如下问题 java.lang.IllegalStateException: Restarter has not been initialized at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE] at org.springframework.boot.devtools.restart.Rest
道可道非常道
2019/05/05
2.5K0
java.lang.IllegalStateException: Restarter has not been initialized
java.lang.IllegalStateException: Service id not legal hostname
SpringCloud服务,在启动两个A服务,然后使用B服务通过feign调用A时,出现以下错误:
IT云清
2019/01/22
9.5K0
SpringBootTest.WebEnvironment.NONE Caused by: java.lang.IllegalStateException: No ServletContext set
不管@EnableWebMvc注解在哪个Configuration类中,只要使用了@EnableWebMvc这个注解,就不能随意使用webEnvironment= SpringBootTest.WebEnvironment.NONE这个配置
johnhuster的分享
2022/03/29
1.8K0
java.lang.IllegalStateException: getOutputStream() has already been called for this response
https://stackoverflow.com/questions/33982515/handlerinterceptor-getoutputstream-has-already-been-called-for-this-response
一个会写诗的程序员
2018/08/17
2.1K0
flink中获取执行计划报错IllegalStateException Cteate breakpoint
flink执行计划网址:https://flink.apache.org/visualizer/
CoreDao
2021/04/12
3450
flink中获取执行计划报错IllegalStateException Cteate breakpoint

相似问题

方法getReadableDatabase()上的错误getReadableDatabase()

20

getReadableDatabase上的android指针异常

10

getReadableDatabase上的安卓DatabaseObjectNotClosedException

22

getWritableDatabase() VS getReadableDatabase()

50

NullPointerException on getReadableDatabase()

53
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档