【发布时间】:2017-06-10 15:33:50
【问题描述】:
我想通过 Messenger 机器人发送一系列消息,中间有一段时间,并显示打字效果。目前我有这个代码:
sendTextMessage(recipientId, `Howdy ${snapshot.child("userDetails/firstName").val()}! I’m Marco :)`)
sendSenderAction(recipientId, "typing_on")
setTimeout(()=>sendTextMessage(recipientId, "Botting Marco."),1000)
sendSenderAction(recipientId, "typing_on")
setTimeout(()=>sendTextMessage(recipientId, "Let me show you some of my recent work. This is the first portfolio bot ever. (afaik) :p"),5000)
sendSenderAction(recipientId, "typing_on")
setTimeout(()=>sendTextMessage(recipientId, "How many minutes do you have?"),7000)
userRef.update({stage:1, class:1, awaiting: true})
但是,由于某些原因,没有出现打字效果。我是否需要使用 Promises 来确保在前一行完成时执行每一行?你能举个例子吗?
【问题讨论】:
-
不,你不需要使用 Promise,任何类型的回调机制都可以正常工作 - Promise 只是让事情变得不那么混乱,因为它们可以被链接起来,所以,没有“厄运金字塔”或代码中的“回调地狱”
标签: javascript settimeout es6-promise facebook-messenger