Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8701

Promise { } in Javascript / async await function / return value VS then result #node #javascript

$
0
0

非同期の関数 ( async ) では返り値が Promiseとなるので、直接返り値を利用できない
then で結果が出たことを待ち受けて、その中で関数の返り値を利用する

functionsleep(ms){returnnewPromise(resolve=>setTimeout(resolve,ms));}asyncfunctionf(){awaitsleep(1000);return"XXX";}constresult=f()console.log(result)// Promise { <pending> }f().then(result=>{console.log(result)});// XXX

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/3163


Viewing all articles
Browse latest Browse all 8701

Trending Articles