LoginSignup
15
15

More than 5 years have passed since last update.

async/awaitの不便なところ

Last updated at Posted at 2016-07-01
function sleep(ms) {
  return new Promise((resolve)=> {
    setTimeout(()=> {
      resolve();
    }, ms)  
  })
}

class Human {
  async sayHello() {
    await sleep(1000);
    console.log('hello')
    return this;
  }
  async sayBye() {
    await sleep(1000);
    console.log('bye');
    return this;
  }
}


(async ()=> {
  await new Human().sayHello().sayBye();
})();
// => helloしか出力されない

asyncのメソッドをメソッドチェーンで書くのは不可能な模様?
babelの仕様なのかasync/awaitの仕様なのかは謎
return await this;のようにしてもダメでした

playground

15
15
8

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
15
15