• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

162
Views
How to handle multiple async operations using promises?

I have some code with 2 functions. taskA requires some value and gets that value via a callback that returns the value inside a promise. It then returns the result of doing something with that value via a second callback. taskB provides the value via a callback that is also passed as a parameter, but this callback is synchronous.

How can I get the value from taskB and pass it to taskA, and get the result to pass it back to taskB? I can modify taskA, but taskB must provide its value and obtain a result via a synchronous callback.

async function taskA(requestValue: () => Promise<Value>, provideResult: (result: Result) => void) {
  while(someCondition) {
    let value: Value = await requestValue();
    let result: Result = processValue(value);
    provideResult(result);
  }
}

function taskB(valueCallback: (value: Value) => Result) {
  let state: State = someState;
  ...
  let handleEvent = (event) => {
    let result: Result = valueCallback(convertToValue(event));
    state = updateState(result);
  }
}

Here's a sequence diagram of the behaviour I want to enable: sequence diagram

about 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error