Is it an anti-pattern to use async/await inside of a new Promise() constructor?

Javascriptnode.jsAsynchronousAsync Await

Javascript Problem Overview


I'm using the async.eachLimit function to control the maximum number of operations at a time.

const { eachLimit } = require("async");

function myFunction() {
 return new Promise(async (resolve, reject) => {
   eachLimit((await getAsyncArray), 500, (item, callback) => {
     // do other things that use native promises.
   }, (error) => {
     if (error) return reject(error);
     // resolve here passing the next value.
   });
 });
}

As you can see, I can't declare the myFunction function as async because I don't have access to the value inside the second callback of the eachLimit function.

Javascript Solutions


Solution 1 - Javascript

You're effectively using promises inside the promise constructor executor function, so this the Promise constructor anti-pattern.

Your code is a good example of the main risk: not propagating all errors safely. Read why there.

In addition, the use of async/await can make the same traps even more surprising. Compare:

let p = new Promise(resolve => {
  ""(); // TypeError
  resolve();
});

(async () => {
  await p;
})().catch(e => console.log("Caught: " + e)); // Catches it.

with a naive (wrong) async equivalent:

let p = new Promise(async resolve => { ""(); // TypeError resolve(); });

(async () => {
  await p;
})().catch(e => console.log("Caught: " + e)); // Doesn't catch it!

Look in your browser's web console for the last one.

The first one works because any immediate exception in a Promise constructor executor function conveniently rejects the newly constructed promise (but inside any .then you're on your own).

The second one doesn't work because any immediate exception in an async function rejects the implicit promise returned by the async function itself.

Since the return value of a promise constructor executor function is unused, that's bad news!

Your code

There's no reason you can't define myFunction as async:

async function myFunction() {
  let array = await getAsyncArray();
  return new Promise((resolve, reject) => {
    eachLimit(array, 500, (item, callback) => {
      // do other things that use native promises.
    }, error => {
      if (error) return reject(error);
      // resolve here passing the next value.
    });
  });
}

Though why use outdated concurrency control libraries when you have await?

Solution 2 - Javascript

I agree with the answers given above and still, sometimes it's neater to have async inside your promise, especially if you want to chain several operations returning promises and avoid the then().then() hell. I would consider using something like this in that situation:

const operation1 = Promise.resolve(5)
const operation2 = Promise.resolve(15)
const publishResult = () => Promise.reject(`Can't publish`)

let p = new Promise((resolve, reject) => {
  (async () => {
    try {
      const op1 = await operation1;
      const op2 = await operation2;

      if (op2 == null) {
         throw new Error('Validation error');
      }

      const res = op1 + op2;
      const result = await publishResult(res);
      resolve(result)
    } catch (err) {
      reject(err)
    }
  })()
});

(async () => {
  await p;
})().catch(e => console.log("Caught: " + e));
  1. The function passed to Promise constructor is not async, so linters don't show errors.
  2. All of the async functions can be called in sequential order using await.
  3. Custom errors can be added to validate the results of async operations
  4. The error is caught nicely eventually.

A drawback though is that you have to remember putting try/catch and attaching it to reject.

Solution 3 - Javascript

BELIEVING IN ANTI-PATTERNS IS AN ANTI-PATTERN

Throws within an async promise callback can easily be caught.

(async () => {
	try {
		await new Promise (async (FULFILL, BREAK) => {
			try {
				throw null;
			}
			catch (BALL) {
				BREAK (BALL);
			}
		});
	}
	catch (BALL) {
		console.log ("(A) BALL CAUGHT", BALL);
		throw BALL;
	}
}) ().
catch (BALL => {
	console.log ("(B) BALL CAUGHT", BALL);
});

or even more simply,

(async () => {
	await new Promise (async (FULFILL, BREAK) => {
		try {
			throw null;
		}
		catch (BALL) {
			BREAK (BALL);
		}
	});
}) ().
catch (BALL => {
	console.log ("(B) BALL CAUGHT", BALL);
});

Solution 4 - Javascript

I didn't realized it directly by reading the other answers, but what is important is to evaluate your async function to turn it into a Promise.

So if you define your async function using something like:

let f = async () => {
    // ... You can use await, try/catch, throw syntax here (see answer of Vladyslav Zavalykhatko) .. 
};

your turn it into a promise using:

let myPromise = f()

You can then manipulate is as a Promise, using for instance Promise.all([myPromise])...

Of course, you can turn it into a one liner using:

(async () => { code with await })()

Solution 5 - Javascript

static getPosts(){
    return new Promise( (resolve, reject) =>{
        try {
            const res =  axios.get(url);
            const data = res.data;
            resolve(
                data.map(post => ({
                    ...post,
                    createdAt: new Date(post.createdAt)
                }))
            )
        } catch (err) {
            reject(err);                
        }
    })
}

remove await and async will solve this issue. because you have applied Promise object, that's enough.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser5487299View Question on Stackoverflow
Solution 1 - JavascriptjibView Answer on Stackoverflow
Solution 2 - JavascriptVladyslav ZavalykhatkoView Answer on Stackoverflow
Solution 3 - JavascriptBryan GraceView Answer on Stackoverflow
Solution 4 - JavascripttobiasBoraView Answer on Stackoverflow
Solution 5 - JavascriptAlainView Answer on Stackoverflow