Convert Promise to async function #202

Open
nt1m opened this Issue Dec 29, 2016 · 8 comments

Projects

None yet

4 participants

@nt1m
nt1m commented Dec 29, 2016

Input:

var promise = function() {
  return new Promise((resolve) => function() {
    setTimeout(resolve, 200, 4);
  });
};
var aF = function() {
  return new Promise(resolve => {
    promise.then((data) => {
      resolve(data);
    });
  });
}

Expected output:

let promise = () => new Promise((resolve) => function() {
    setTimeout(resolve, 200, 4);
  });
};

let aF = async () => {
  return await promise;
};
@nt1m
nt1m commented Dec 29, 2016

Also:

let bob = {  
  asyncFunc: async function() {
  }
};

should be changed to:

let bob = {
  async asyncFunc() {}
}
@nene
Collaborator
nene commented Dec 29, 2016

The first example looks weird. The aF function is basically pass-through - why would anybody write such code. It also doesn't actually work as the function promise is never called. My gut feeling is that implementing any Promise to async-await conversion will be pretty complicated task.

The other proposal should automatically work once we have support for parsing the async-await syntax, and that's really the part behind which all of this is waiting. It also requires Recast to support that additional syntax - it allows to swap the parser and use e.g. babylon, but I think it only really supports syntax up to ES2016.

@nene nene changed the title from Convert to async function to Convert Promise to async function Dec 29, 2016
@nt1m
nt1m commented Dec 30, 2016

@nene Sorry, the first example was some quick one-off code. Maybe this blog post has better examples: https://hacks.mozilla.org/2016/12/asyncawait-arrive-in-firefox/

@nt1m
nt1m commented Dec 30, 2016 edited

instead of nested Promise callbacks, we want async/await

@tunnckoCore

@nene Babylon supports everything :D https://github.com/tunnckoCore/parse-function/blob/master/test.js is proof that it works for async/await.

@nene
Collaborator
nene commented Dec 30, 2016

@tunnckoCore Ah, I guess my comment was pretty ambiguous. My concern wasn't about babylon, but about Recast supporting the things babylon supports.

@hzoo
hzoo commented Jan 2, 2017

@nene recast should support everything Babel does after my PR to update it to support Babel 6 changes; if not we can make a issue/pr

@nene
Collaborator
nene commented Jan 2, 2017

Ah, that's great to hear. I definitely have to try it out then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment