ActionsSDK v2対応: 非同期処理の書き方(雑記)
インテント受け取り後に、非同期処理をPromiseチェーンで処理を書くと、下記のエラーが出てどうにも動かない。
No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?
Promiseそんなに習熟してないので、コードが悪いのかと延々と検証していたが、
一番短いコードでも動かなかったので何だこりゃと思って調査した。
原因
Google公式によると、v2での非同期処理はインテントハンドラに返さないとダメということだ。
参考リンク:v2移行ガイド
Actions on Google Node.js Client Library Version 1 Migration Guide | Actions on Google | Google Developers
重要な部分の引用
Additionally, async tasks now have built-in direct support in the library. To perform an async task, you must return a Promise to the intent handler. (さらに、非同期タスクにはライブラリ内の組み込みの直接サポートが追加されました。 非同期タスクを実行するには、インテントハンドラにPromiseを返す必要があります。)
くそー、何回も読んだページだけど、全然理解して無かったということか…。
その他の参考記事
StackOverFlow
stackoverflow.com
サンプルソース
app.intent('actions.intent.MAIN', conv => { return handleConversation(conv) }) function handleConversation(conv) { // Promiseを返す return ( Promise.resolve(conv) .then(checkUserData) // .then(SuperFunc) // .then(UltraFunc) // .then(DragonFunc) .then(conv => { conv.close('デバッグです') return }) .catch(err => { console.log('handleConversation', err) conv.close( 'アプリに問題が発生しました。後ほどもう一度お試しください。終了します。' ) return }) ) } function checkUserData(conv) { return new Promise(resolve => { // 何らかの処理 resolve(conv) }) }
動くと嬉しい。