babel / kneden

Transpile ES2017 async/await to vanilla ES6 Promise chains: a Babel plugin
ISC License
514 stars 41 forks source link

Bug with variable order? #42

Closed nodegin closed 8 years ago

nodegin commented 8 years ago

Here's a quick test, consider the following code:

function getDelayedNull() {
  // Wait 250ms to retry
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(null)
    }, 1000)
  })
}
async function test() {
  const a = Math.PI
  const n = await getDelayedNull()
  console.log(`${a}`)
}
test()

Which transpiles to:

function test() {
  return Promise.resolve().then(function () {
    const a = Math.PI;
    return getDelayedNull();
  }).then(function (_resp) {
    const n = _resp;
    console.log(`${ a }`);
  });
}
test();

The Math.PI doesn't getting logged,

If I reorder the line of const a = Math.PI to after the await statement:

async function test() {
  const n = await getDelayedNull()
  const a = Math.PI
  console.log(`${a}`)
}

Which would prints out the PI successfully.

I suppose both should log the PI in console correctly

Is this a bug? thanks.

marten-de-vries commented 8 years ago

It's not a bug, as mentioned in the README:

Note: Kneden only supports transpiling ES5 with the addition of async/await. If you're using other ES6 features (like arrow functions, let/const, classes, etc.), make sure you transpile them down to valid ES5 code first using the babel es2015 preset. See #19 for more information.

Thanks for testing and reporting, though!