Is it possible to resolve async function without return keyword
Asked Answered
I

3

24

I started to use ES7 feature async/await , which gives the best approach to deal with asynchronous tasks, and makes your code cleaner and readable.

However it doesn't give you an access to Promise, created by async function, so if you do some async request in your async function you should promisify it, then await it and then return the result. I mean this:

async function doStuff() {
    //stuff...
    var value = await new Promise(function(resolve) {
        $.get('http://some/url/...', function(result) {
            // stuff...
            resolve(result);
        });
    });
    return value;
}

What if you could find a pointer to the Promise created by function so your code could look like:

async function doStuff() {
    //stuff...
    var p = arguments.callee.promise;
    $.get('http://some/url/...', function(result) {
        // stuff...
        p.resolve(result);
    });
}

or even:

async function doStuff() {
    //stuff...
    $.get('http://some/url/...', function(result) {
        // stuff...
        async.resolve(result);
    });
}

This way you don't need to directly access Promises API what makes your code totally focused on task without any besides.

Insensibility answered 9/11, 2016 at 1:21 Comment(3)
I'm thinking you're missing the point of async/await. Can you produce a scenario where simply doing result = await $.get(someUrl); doStuffWith(result) is not sufficient? The whole point of the proposal is not to have to deal with callbacks.Hoist
I just thought if you are in promise already why do you need to create another. Bergi below responded clearly - No. =)Insensibility
Consider this if you happened to drop by here for your quirky code: #26150732.Declarative
K
24

Is it possible to resolve async function without return keyword

No.

There is no way to get a reference to the promise that the call to the async function created, but there really is no need to access that either (and btw, you cannot .resolve() a promise, you'd actually need to get access to the promise's resolving functions).

The whole point of async/await is to play nice with promises and other thenables. The idea is that every asynchronous function returns a promise, and that you don't have to promisify anything (but if you really have to, do it separately) - and in fact, $.get does return a (jQuery) promise. So simply write

async function doStuff() {
    //stuff...
    var result = await $.get('http://some/url/...');
    // stuff...
    return someValue;
}

If you really have a callback-taking function, use a simple

async function doStuff() {
    // stuff…
    return new Promise(function(resolve, reject) {
        $.get({
            url: 'http://some/url/...',
            success: resolve,
            error: reject
            // don't do other "stuff" in here
        });
    });
}
Kodok answered 9/11, 2016 at 1:40 Comment(0)
J
3

When you're returning a promise from a function there is no need for the function to be async, actually it's even bad because you are creating two promise!

function doStuff() {
    return new Promise(function(resolve, reject) {
        $.get('http://some/url/...', result => resolve(result));
    });
}

async function main() {
    const result = await doStuff();
}
Jaynes answered 28/7, 2017 at 7:39 Comment(1)
async functions wrap returned values into a Promise only if the value is not already explicitly a Promise. You will not be creating two nested promises by returning a promise. It is not uncommon to see inside async functions a return Promise.all(...); statement. In such situations Promise.all() returns a Promise, and allows for multiple parallel operations to terminate before the Promise returned by the async function resolves.Bait
T
0

How about creating your async functions and then using await when you invoke them.

  //define
  async promiseFunction(foo) {
    return otherPromiseFunction(foo);
  async promiseFunction2(foo) {
    return '10';
  }
  async promiseFunction3(foo) {
    return new Promise((resolve, reject) => {
      resolve('foo')
    })
  }
//invoke
  anotherPromiseFunction(bar).then(async foo => {
    const fooResult = await promiseFunction(foo);
    return fooResult; 
  })
Thinia answered 16/8, 2018 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.