Sobes.tech
Junior — Senior

Wrapper around a callback function that returns a Promise

livecode

Task condition

In the project, there is a function asyncAuth that takes a callback. The callback can receive an error and data received from the server after completing the asynchronous operation. It is necessary to implement a wrapper function auth that calls asyncAuth but returns a Promise object.

 import asyncAuth from '~';

/**
  The function `asyncAuth(callback)` accepts a callback, which can
  receive an error (as the first argument) and data
  from the server (as the second argument).
  asyncAuth(error, data) => {});
  *
  You need to implement the function `auth()`, 
  which calls `asyncAuth()`, but returns a Promise.
  *
  @returns (Promise)
  */
function auth() {
    asyncAuth((error, data) => {});
}

/**
  The function `tryAuth()` uses `auth()` and, in case of an error,
  makes N additional attempts.
  If all attempts fail, return the last error.
  *
  @returns (Promise)
  */
function tryAuth(n) {
}