1

I have created a simply function with sub functions that returns balance and you can add an amount to update your balance.

const bankAccount = (initialBalance) => {
  let balance = initialBalance;

  return {
    getBalance: function() {
      return balance;
    },
    deposit: function(amount) {
      balance += amount;
      return balance;
    },
  };
};

const account = bankAccount(100);

account.getBalance();
account.deposit(10);

My question is I want to make this function asynchronous, my question is should the overarching function be wrapped in a promise or do the sub functions need to be wrapped in a promise.

This is the kind of approach I was thinking. Is this correct?

async function bankAccount(initialBalance) {
  let balance = initialBalance;
  return await new Promise((resolve, reject) => {
    if ("some error") {
      reject("something went wrong");
    }
    return {
      getBalance: function () {
        resolve(balance);
      },
      deposit: function (amount) {
        balance += amount;
        resolve(balance);
      },
    };
  });
}
hulike2286
  • 61
  • 5
  • Why do you want to make `bankAccount` async? Wouldn't it be the `getBalance` and `deposit` functions that might need to be async? – jarmod Nov 09 '22 at 16:27
  • @jarmod would you say that then the outer function bankAccount should be just a normal function but the inner functions need to be async and then have the promises within them? – hulike2286 Nov 09 '22 at 16:44
  • What needs to be async about `bankAccount`? It's effectively just a class with one attribute and two methods. Constructing a bank account simply initializes a local balance and binds and returns a couple of function pointers. It doesn't actually appear to call those functions. – jarmod Nov 09 '22 at 16:54
  • I guess then the question would be, how can I add promise support to this? I.e. I want to be able to do something like `account.deposit(Promise.resolve(10)) – hulike2286 Nov 09 '22 at 17:20
  • Promises have a role when you have asynchronous dependencies. Which are they? Just introducing promises for the sake of it makes no sense. – trincot Nov 09 '22 at 20:50

2 Answers2

1

Here's one way you can do this with async methods for deposit and balance. I've simulated asynchronous calls using the sleep method that triggers a response after 2 seconds.

function sleep(retval, ms = 2000) {
  return new Promise(function (resolve, reject) {
    setTimeout(() => {
      resolve(retval);
    }, ms);
  });
}

const bankAccount = (initialBalance) => {
  let balance = initialBalance;

  return {
    getBalance: async () => sleep(balance),
    deposit: async (amount) => {
      balance += amount;
      return sleep(balance);
    },
  };
};

// An async IIFE, needed unless we have top-level await support
(async () => {
  const account = bankAccount(100);
  console.log("Initial balance:", await account.getBalance());
  console.log("Deposit 10, new balance:", await account.deposit(10));
  console.log("Deposit 10, new balance:", await account.deposit(10));
})();
jarmod
  • 71,565
  • 16
  • 115
  • 122
0

That's right it is the right way to use promises you just have some details that I fixed in the following code

async function bankAccount(initialBalance) {
  return await new Promise((resolve, reject) => {
  let balance = initialBalance;
    if ("some error") {
      reject("something went wrong");
    }
    return {
      getBalance: function () {
        resolve(balance);
      },
      deposit: function (amount) {
        balance += amount;
        resolve(balance);
      },
    };
  });
}
Moussa Bistami
  • 929
  • 5
  • 15