前提・実現したいこと
Azure Blob Trigger Apiを実装しています。目的は同期です。
async,await,Promiseについて、もっと綺麗に整理しようと思いますが、なかなか完璧にならないですね。
よいアドバイスがあれば、ぜひ!
整理前
index.js
nodejs
1var request = require('request-promise'); 2module.exports = async function (context, myBlob) { 3 context.log("JavaScript blob trigger function processed blob \n Name:", context.bindingData.name, "\n Blob Size:", myBlob.length, "Bytes"); 4 var optionsLatest = { 5 url: 'http://127.0.0.1/blockchain/latest', 6 json: true 7 } 8 await request(optionsLatest) 9 .then(function (body) { 10 context.log(body); 11 }) 12 .catch(function (err) { 13 context.log(err); 14 }); 15}
整理後
index.js
nodejs
1var optionsBC = require('./options'); 2module.exports = async function (context, myBlob) { 3 context.log("JavaScript blob trigger function processed blob \n Name:", context.bindingData.name, "\n Blob Size:", myBlob.length, "Bytes"); 4 var res = await optionsBC.getLatestRes(); 5 context.log(res); 6}
options.js
nodejs
1var request = require('request-promise'); 2async function getLatestRes() { 3 var optionsLatest = { 4 url: 'http://127.0.0.1/blockchain/latest', 5 json: true 6 } 7 return await request(optionsLatest) 8 .then(function (body) { 9 return body; 10 }) 11 .catch(function (err) { 12 return null; 13 }); 14} 15 16exports.getLatestRes = getLatestRes
試したこと
取り合えず、↑の整理後のものになっています。
完璧になりません(index.jsにawaitをつけるのはいやだ)が、仕方がありません。
nodejsであれば、多少我慢しないといけないですね。
いろんなパターンで試してみました、まとめて言うと:
①Promise を返す関数の呼び出しに await を付ける
②await を使う関数に async を付ける
ですので、index.jsにawaitを省略できません。そうしないと同期にならないです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。