caolan / caolan/async

Correct way to obtain result using async functions in queue

Open
#2,025 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
28.1k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

Hello,
First of all, thank you for this great library!

This is not an issue but a question.

I'm using the queue functionality in `async` to commit database transactions. Below is an example of how I currently use it:

```typescript
import { queue, QueueObject } from "async";

type QueueTask = { name: string };

export class AsyncLabWithoutAsyncFunction {
private static _transactionQueue: QueueObject;

private static init(): void {
AsyncLabWithoutAsyncFunction._transactionQueue = queue((task: QueueTask, callback): void => {
try {
const myNameInUpperCase: string = this.nameInUpperCase(task.name);
callback(null, myNameInUpperCase);
} catch (err) {
callback(null, "error");
}
}, 1);
}

static async perform(nameInLowerCase: string): Promise {
if (AsyncLabWithoutAsyncFunction._transactionQueue == null) {
AsyncLabWithoutAsyncFunction.init();
}

return new Promise((resolve) => {
try {
AsyncLabWithoutAsyncFunction._transactionQueue.push({ name: nameInLowerCase }, (err, response: string) => {
console.log(`response = ${response}`);
console.log(`name = ${nameInLowerCase}`);
resolve(response);
});
} catch (err) {
resolve(null);
}
});
}

static nameInUpperCase(nameInLowerCase: string): string {
return nameInLowerCase.toUpperCase();
}
}

```

Now, I need to update my code to pass an **async function** to the queue, in which the callback isn't passed. From the documentation:

> Using ES2017 async functions
Async accepts async functions wherever we accept a Node-style callback function. However, we do not pass them a callback, and instead use the return value and handle any promise rejections or errors thrown.

I struggled to find a way to retrieve the result of the database transaction, but I think I found a solution by adding `.then(callback)` when pushing to the queue:

```typescript
import { queue, QueueObject } from "async";

type QueueTask = { name: string };

export class AsyncLabWithAsyncFunction {
private static _transactionQueue: QueueObject;

private static init(): void {
AsyncLabWithAsyncFunction._transactionQueue = queue(async (task: QueueTask): Promise => {
try {
const myNameInUpperCase: string = await this.nameInUpperCase(task.name);
return myNameInUpperCase;
} catch (err) {
return "error";
}
}, 1);
}

static async perform(nameInLowerCase: string): Promise {
if (AsyncLabWithAsyncFunction._transactionQueue == null) {
AsyncLabWithAsyncFunction.init();
}

return new Promise((resolve) => {
try {
AsyncLabWithAsyncFunction._transactionQueue.push({ name: nameInLowerCase }).then((response: string) => {
console.log(`response = ${response}`);
console.log(`name = ${nameInLowerCase}`);
resolve(response);
});
} catch (err) {
resolve(null);
}
});
}

static nameInUpperCase(nameInLowerCase: string): Promise {
return new Promise((resolve) => {
setTimeout(() => {
resolve(nameInLowerCase.toUpperCase());
}, 1_000);
});
}
}

```

**Question:**
Is this the correct way to obtain the result when using **async functions** in `queue`?

Thanks a lot!
Niclas

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue names no repository files or tests. Start with the documented async-function behavior and the queue push API, then clarify how a returned task value is obtained; done means the documentation or issue has an unambiguous answer, supported by the existing API behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
developer-experience
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.