LoginSignup
4
8

More than 1 year has passed since last update.

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

Last updated at Posted at 2020-05-13

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

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function f() {
  await sleep(1000);
  return "XXX";
}

const result = f()
console.log(result)
// Promise { <pending> }

f().then(result => {
  console.log(result)
});
// XXX

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

4
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
8